Fizz Buzz Problem in C++
Fizz Buzz is a popular coding challenge often used in job interviews to assess a candidate's basic programming skills and problem-solving abilities. This problem, though simple in nature, serves as a ‘fundamental test’ of one's understanding of programming constructs like ‘conditionals’ and ‘loops.’
While the basic Fizz Buzz problem is straightforward, it has inspired numerous variations that make it more challenging and thought-provoking. These variations test not only a programmer's ability to handle the basic rules but also their adaptability and creativity when facing many complex problems. Therefore, the "Fizz Buzz" problem, though deceptively simple, is a valuable tool in assessing a programmer's competence and skills. To solve the Fizz Buzz problem, you need to iterate through numbers from “1 to n” and apply the correct logic.
The Fizz Buzz problem can be customized and extended in numerous ways. The key lies in understanding the problem's underlying ‘logic’ and adjusting the conditions and outcomes to meet your specific requirements. One common application is in ‘programming interviews,’ where it assesses a candidate's fundamental coding skills and ability to think logically.
The algorithm for Fizz Buzz is given by
Algorithm:
for i from 1 to N:
if i is divisible by 3 and i is divisible by 5:
print "Fizz Buzz"
else if i is divisible by 3:
print "Fizz"
else if i is divisible by 5:
print "Buzz"
else:
print I
Let us see the methods to solve the problem
Naïve Method
Code:
// Naïve Method for fizz Buzz
//Header declaration
#include <iostream>
// main Block
int main() {
int N = 6; // Change N to the desired range
// for loop
for (int i = 1; i <= N; i++) {
if (i % 3 == 0 && i % 5 == 0) {
std::cout << "FizzBuzz ";//Output of required value
} else if (i % 3 == 0) {
std::cout << "Fizz ";// Output of Required value
} else if (i % 5 == 0) {
std::cout << "Buzz ";// Output of Required value
} else {
std::cout << i << " ";
}
}
// end of for
return 0;
}
Output
![Fizz Buzz Problem in C++](https://static.tutorialandexample.com/cpp/fizz-buzz-problem-in-cpp1.png)
Therefore, for an input of 6, we will be getting an output as shown below. The sequence starts from 1 and ends at 6, it prints the corresponding values according to the logic.
Using Ternary Operators
Code:
//Fizz Buzz program using operators
//Header Declaration
#include <iostream>
// Main Block
int main() {
int N = 7; // Change N to the desired range
// for loop
for (int i = 1; i <= N; i++) {
std::cout << (i % 3 == 0 && i % 5 == 0 ? "FizzBuzz " : i % 3 == 0 ? "Fizz " : i % 5 == 0 ? "Buzz " : std::to_string(i) + " ");
}
// end of for loop
return 0;
}
Output:
![Fizz Buzz Problem in C++](https://static.tutorialandexample.com/cpp/fizz-buzz-problem-in-cpp2.png)
The above is the output for an input of 7. It will be running a loop from i= 1 to 7 and then will be printing the values according to the logic.
Using Switch
Code
#include <iostream>
int main() {
int N = 8; // Change N to the desired range
// for loop
for (int i = 1; i <= N; i++) {
switch (0) {
case i % 3: std::cout << "Fizz ";// outputting the required value
case i % 5: std::cout << "Buzz "; // outputting the required value
default: std::cout << i << " "; // outputting the required value
}
}
// end of for loop
return 0;
}
Output:
![Fizz Buzz Problem in C++](https://static.tutorialandexample.com/cpp/fizz-buzz-problem-in-cpp3.png)
Function Method
Code:
#include <iostream>
#include <string>
// Function Body
std::string fizzBuzz(int num) {
//if block
if (num % 3 == 0 && num % 5 == 0) {
return "FizzBuzz ";
//else if block
} else if (num % 3 == 0) {
return "Fizz ";
} else if (num % 5 == 0) {
return "Buzz ";
//Else Block
} else {
return std::to_string(num) + " ";
}
}
//main Block
int main() {
int N = 9; // Change N to the desired range
// for loop
for (int i = 1; i <= N; i++) {
std::cout << fizzBuzz(i);
}
//end of for loop
return 0;
}
Output:
![Fizz Buzz Problem in C++](https://static.tutorialandexample.com/cpp/fizz-buzz-problem-in-cpp4.png)
The above takes an output of 9 and goes through a function that has the main logic, then prints the values according to the logic.
Array-Based
Code
#include <iostream>
#include <string>
// main Block
int main() {
int N = 7; // Change N to the desired range (in this case, 7)
// Array of the required output
std::string output[8] = { "", "1 ", "2 ", "Fizz ", "4 ", "Buzz ", "Fizz ", "7 " };
// for loop
for (int i = 1; i <= N; i++) {
std::cout << output[i];// printing required output
}
// end of for
return 0;
}
Output:
![Fizz Buzz Problem in C++](https://static.tutorialandexample.com/cpp/fizz-buzz-problem-in-cpp5.png)
For an input of 7, the code gives an output as shown above; it stores the output in the form of an array and prints the values based on the logic.