Sunday, September 20, 2015

FizzBuzz Problem

Write a short program that prints the numbers from 1 to 100. 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".

examples : 
1
2
3 => Fizz
4
5 => Buzz
...
...
15 => FizzBuzz
...
...
so on..


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public static void main(String[] args){
      
      for(int x=1;x<=100;x++){
      if(x%3==0){
       System.out.print("Fizz");
       if(x%5==0){
        System.out.print("Buzz");
       }
       System.out.println();
      }
      else if(x%5==0)
       System.out.println("Buzz");
      else
       System.out.println(x);
      
     }
   
   }

No comments:

Post a Comment