cplusplus
cplusplus

 

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

  1. Open your C++ development environment or text editor (e.g., Visual Studio, Code::Blocks, or an IDE of your choice).
  2. Create a new file and paste the provided code into it.
  3. Save the file with a .cpp extension (for example, fizzbuzz.cpp).
  4. 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).
  5. Run the compiled program (e.g., ./fizzbuzz on Unix-based systems or simply run it within your IDE).
  6. 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++.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)