DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kuldeep has posted 1 posts at DZone. View Full User Profile

FizzBuzz Problem

02.12.2013
| 3475 views |
  • submit to reddit
Integer n = 100;
String[] format = new String[]{"%d\n","fizz\n","buzz\n","fizzbuzz"};
for(int i=1;i< n; i++){
    System.out.printf(format[((i%3==0)?1:0) + 2 *((i%5==0)?1:0)] + "",i);
}

The Code for FizzBuzz problem in java. For a given n, it prints all the number from 1 to n, But for the number divisible by 3 it prints "fizz", for number divisible by 5 it prints "buzz", and for number divisible by both 3 and 5 it prints "fizzbuzz"