The FizzBuzz problem is a classic exercise in programming that helps to test basic understanding of loops, conditionals, and modular arithmetic. In this problem, we are required to print numbers from 1 to 100. However, for multiples of 3, we print “Fizz” instead of the number, for multiples of 5, we print “Buzz” instead, and for numbers that are multiples of both 3 and 5, we print “FizzBuzz”.
Objective
The main objective of this exercise is to implement a solution using C++ that adheres to the following conditions:
- Print the numbers from 1 to 100.
- For numbers that are multiples of 3, print “Fizz”.
- For numbers that are multiples of 5, print “Buzz”.
- For numbers that are multiples of both 3 and 5, print “FizzBuzz”.
- For all other numbers, print the number itself.
C++ Code
#include <iostream> // Main function to implement FizzBuzz int main() { // Loop through numbers from 1 to 100 for(int i = 1; i <= 100; i++) { // Check if the number is divisible by both 3 and 5 if(i % 3 == 0 && i % 5 == 0) { std::cout << "FizzBuzz" << std::endl; } // Check if the number is divisible by 3 else if(i % 3 == 0) { std::cout << "Fizz" << std::endl; } // Check if the number is divisible by 5 else if(i % 5 == 0) { std::cout << "Buzz" << std::endl; } // Otherwise, print the number itself else { std::cout << i << std::endl; } } return 0; }
Explanation of the Code
The program uses a for
loop to iterate through the numbers from 1 to 100. For each number, the program performs the following checks:
- If the number is divisible by both 3 and 5 (using the modulo operator
%
), the program prints “FizzBuzz”. - If the number is divisible only by 3, the program prints “Fizz”.
- If the number is divisible only by 5, the program prints “Buzz”.
- If none of the above conditions are met, the program prints the number itself.
The program is structured to evaluate divisibility starting from the most restrictive case (divisible by both 3 and 5) and working down to the least restrictive (print the number itself). This ensures that numbers like 15, which are divisible by both 3 and 5, will correctly output “FizzBuzz”.
How to Run the Program
- Open your C++ development environment or text editor (e.g., Visual Studio, Code::Blocks, or an IDE of your choice).
- Create a new file and paste the provided code into it.
- Save the file with a
.cpp
extension (for example,fizzbuzz.cpp
). - Compile the code using a C++ compiler (e.g.,
g++ fizzbuzz.cpp -o fizzbuzz
on a Unix-based system, or use your IDE’s build feature). - Run the compiled program (e.g.,
./fizzbuzz
on Unix-based systems or simply run it within your IDE). - The program will print the FizzBuzz sequence for numbers 1 through 100.
Conclusion
The FizzBuzz problem is a simple yet effective way to practice basic control structures in programming. By using conditional statements and loops, we can efficiently solve this problem and output the desired results. This program demonstrates how to handle basic divisibility checks and output different results based on those conditions in C++.