Java
Java

 

The FizzBuzz problem is a classic programming exercise that tests a developer’s ability to work with loops and conditionals.
The objective is to print the numbers from 1 to 100, but with a twist: for multiples of 3, we print “Fizz” instead of the number,
for multiples of 5, we print “Buzz”, and for multiples of both 3 and 5, we print “FizzBuzz”.
This exercise helps reinforce the understanding of control flow in programming.

Java Code Implementation


public class FizzBuzz {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                System.out.println("FizzBuzz");
            } else if (i % 3 == 0) {
                System.out.println("Fizz");
            } else if (i % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }
    }
}

Program Structure Explanation

The program consists of a single class named FizzBuzz with a main method, which is the entry point of the Java application.
Here’s a breakdown of the structure:

  • for loop: This loop iterates through the numbers 1 to 100. The variable i takes on each value in this range.
  • if-else statements: Inside the loop, we check:
    • If the number is divisible by both 3 and 5 using i % 3 == 0 && i % 5 == 0. If true, we print “FizzBuzz”.
    • If the number is divisible only by 3 using i % 3 == 0. If true, we print “Fizz”.
    • If the number is divisible only by 5 using i % 5 == 0. If true, we print “Buzz”.
    • If none of the above conditions are met, we print the number itself.

How to Run the Program

To run the FizzBuzz program, follow these steps:

  1. Ensure you have Java Development Kit (JDK) installed on your machine.
  2. Open a text editor or an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA.
  3. Create a new file named FizzBuzz.java.
  4. Copy and paste the Java code provided above into the file.
  5. Save the file.
  6. Open a terminal or command prompt and navigate to the directory where you saved the file.
  7. Compile the program by running the command: javac FizzBuzz.java.
  8. Run the compiled program using the command: java FizzBuzz.
  9. You should see the numbers from 1 to 100 printed with “Fizz”, “Buzz”, and “FizzBuzz” as specified.

 

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 :)