Friday, September 4, 2015

FizzBuzz

Problem

For integers 1 to 100, print 'Fizz' if its divisible by 3, print 'Buzz' if its divisible by 5, 'FizzBuzz' if its divisible by 3 and 5, print the number alone otherwise.

Solution

public class FizzBuzz {

 public void printFizzBuzz(){
  
  String temp;
  for(int i=1; i< 100; i++)
  {
   temp = "";
   if(i%3 == 0) temp = "Fizz";
   if(i%5 == 0) temp += "Buzz";
   if(temp.length() == 0) temp = i + ""; 
   
   System.out.println(temp);
  }
  
 }
 public static void main(String[] args) {

  FizzBuzz fb = new FizzBuzz();
  fb.printFizzBuzz();
 }

}

No comments:

Post a Comment