Fizz Buzz Java
Fizz Buzz is a classic coding interview question and a simple programming task. The task is to print numbers from 1 to n, but for multiples of 3, print “Fizz” instead of the number, and for multiples of 5, print “Buzz” instead of the number. For numbers that are multiples of both 3 and 5, print “FizzBuzz.” Here’s a Java program that implements the Fizz Buzz problem:
public class FizzBuzz {
public static void main(String[] args) {
int n = 100; // Change n to the desired range of numbers
for (int i = 1; i <= n; 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);
}
}
}
}
In this program:
- We start a loop from 1 to
n
. - We use the modulo operator
%
to check if a number is a multiple of 3, 5, or both. - If a number is a multiple of both 3 and 5, we print “FizzBuzz.”
- If a number is only a multiple of 3, we print “Fizz.”
- If a number is only a multiple of 5, we print “Buzz.”
- Otherwise, we print the number itself.
You can change the value of n
to control how many numbers you want to print in the Fizz Buzz sequence.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks