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 fromi = 1
toi = 100
, incrementingi
by 1 after each iteration. - if-else statements: Inside the loop, the program uses a series of
if
andelse 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 numberi
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 ofi
, 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:
- Make sure you have Java installed. You can check by typing
java -version
in the command prompt or terminal. - Create a new file called
FizzBuzz.java
. - Copy and paste the code provided above into the
FizzBuzz.java
file. - Open the command prompt or terminal and navigate to the directory where the file is saved.
- Compile the program by running:
javac FizzBuzz.java
- Run the program using the command:
java FizzBuzz
- You should see the numbers from 1 to 100 printed with the appropriate Fizz, Buzz, or FizzBuzz output.