Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!
Welcome to Coding challenge Day 13: Problem 2! Be sure to post your answers, queries etc in the comments!
Problem: Write a Java program that prints the numbers from 1 to 50. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”
Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Solution
Note the following code is in JAVA
package misc;
public class Program002fizzbuzz {
public static void main (String [] args){
for(int i=1; i<=50; 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);
}
}
}
Happy Learning!!