Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Java Write a Java program that prints the numbers from 1 to 50. But for mu

ID: 3700939 • Letter: U

Question

Using Java
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"
Using Java
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"

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"

Explanation / Answer


//Code to copy
//Java program that prints numbers from 1 to 50
//Fuzz for number divisible by 3
//Fuzz for number divisible by 5
//FuzzBuzz for number divisible by 3 and 5
//Otherwise print number
//PrintDriver.java
public class PrintDriver {

   public static void main(String[] args) {

       int num;
       int MAX=50;
      
       /**Run for loop from 1 to 50*/
       for (num = 1; num <=MAX; num++)
       {
           //check if start num is multiple of 3
               if(num%3==0)
                   System.out.printf("Fizz ");
               //check if start num is multiple of 5
               else if(num%5==0)
                   System.out.printf("Buzz ");
               //check if start num is multiple of 3 and 5
               else if(num%3==0 && num%5==0)
                   System.out.printf("FizzBuzz ");
               else
                   System.out.printf("%d ",num);
                  
       }
   }//end of main
}//end of the class

Sample Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
Fizz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
Fizz
46
47
Fizz
49
Buzz

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote