Java
Java

 

Introduction

In this exercise, we will create a simple Java program to print numbers from 1 to 100, but with a twist.
Instead of printing all numbers normally, we will print numbers following some specific rules:

  • If the number is divisible by 3, print “Fizz”.
  • If the number is divisible by 5, print “Buzz”.
  • If the number is divisible by both 3 and 5, print “FizzBuzz”.
  • If the number is not divisible by either 3 or 5, print the number itself.

The goal of this exercise is to demonstrate basic control flow (loops, conditions) in Java, while also applying modular arithmetic to solve real-world problems.

Objective

The objective of this program is to learn how to:

  • Use loops to iterate through a range of numbers.
  • Apply conditional statements to execute different actions based on specific criteria.
  • Understand the concept of divisibility using modulo operation.
  • Print formatted output based on conditions.

Java Code


                // FizzBuzz Program in Java
                public class FizzBuzz {
                    public static void main(String[] args) {
                        // 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) {
                                System.out.println("FizzBuzz");
                            }
                            // Check if the number is divisible by 3
                            else if (i % 3 == 0) {
                                System.out.println("Fizz");
                            }
                            // Check if the number is divisible by 5
                            else if (i % 5 == 0) {
                                System.out.println("Buzz");
                            }
                            // If the number is not divisible by either 3 or 5, print the number
                            else {
                                System.out.println(i);
                            }
                        }
                    }
                }
            

Explanation of Program Structure

The Java program consists of a FizzBuzz class that contains the main method, which is the entry point of the program.
Here’s a breakdown of the program:

  • for loop: The program uses a for loop to iterate over numbers from 1 to 100. The loop runs from i = 1 to i = 100, incrementing i by 1 after each iteration.
  • if-else statements: Inside the loop, the program uses a series of if and else if statements to check whether the current number is divisible by 3, 5, or both. If it is divisible by both, it prints “FizzBuzz”. If divisible by 3, it prints “Fizz”. If divisible by 5, it prints “Buzz”. Otherwise, it prints the number itself.
  • Modulo operator (%): The program uses the modulo operator to check for divisibility. If i % 3 == 0, this means that the number i is divisible by 3. Similarly, i % 5 == 0 checks divisibility by 5.
  • Output: The output is printed to the console using System.out.println(). Depending on the value of i, it prints either “Fizz”, “Buzz”, “FizzBuzz”, or the number itself.

How to Run the Program

To run this Java program on your local machine, follow these steps:

  1. Make sure you have Java installed. You can check by typing java -version in the command prompt or terminal.
  2. Create a new file called FizzBuzz.java.
  3. Copy and paste the code provided above into the FizzBuzz.java file.
  4. Open the command prompt or terminal and navigate to the directory where the file is saved.
  5. Compile the program by running: javac FizzBuzz.java
  6. Run the program using the command: java FizzBuzz
  7. You should see the numbers from 1 to 100 printed with the appropriate Fizz, Buzz, or FizzBuzz output.
© 2024 Learn Programming. All rights reserved.

 

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