Over a million developers have joined DZone.

FizzBuzz Problem

DZone's Guide to

FizzBuzz Problem

· ·
Free Resource

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"

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);

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}