FizzBuzz Program in Java

In fizz buzz game, we say numbers such that if that number is a multiple of five, we say “fizz”, if multiple of seven, we say “buzz”, multiple of both, we say “fizzbuzz.”

1. What is FizzBuzz?

FizzBuzz is a fun game mostly played in elementary school. The rules are simple:

When our turn arrives, we say the next number (preferably with a French accent). However:

  • If that number is a multiple of five, we say the word “fizz“.
  • If the number is a multiple of seven, we say “buzz“.
  • If it is a multiple of both, we say “fizzbuzz.”

If we mess up, we are out, and the game continues without us.

Please note that different divisors can be used in place of, or in addition to, 5 and 7, and different words or gestures can replace “fizz” or “buzz”. [Ref]

Let’s learn to write a program to simulate this game in Java.

2. FizzBuzz Program using Java Streams

Let’s design a solution using Java 8 Stream API. The following program uses IntStream class which is used to generate a Stream of integers in a range.

We use the ternary operator to check each generated number n in sequence, and check if number is divisible by 7 or 5.

int end = 100;

IntStream.rangeClosed(1, end)
        .mapToObj(
                i -> i % 5 == 0 ? 
                        (i % 7 == 0 ? "FizzBuzz" : "Fizz") : 
                        (i % 7 == 0 ? "Buzz" : i))
        .forEach(System.out::println);

Program Output:

1
2
3
4
Fizz
6
Buzz
...
...
34
FizzBuzz
...
...

3. FizzBuzz Program using For-Loop

If you are still not using Java 8, or you are asked to write the program using loops, then use the following program which replaces the stream with a loop, and ternary operator with if-else statements.

int end = 100;

for (int i = 1; i <= end; i++) {

        if (((i % 5) == 0) && ((i % 7) == 0)) 
        {
                System.out.println("fizzbuzz");
        } else if ((i % 5) == 0) 
        {
                System.out.println("fizz");
        } else if ((i % 7) == 0) 
        {
                System.out.println("buzz");
        } else {
                System.out.println(i); 
        }
}

The program output is similar to the previous solution.

Happy Learning !!

Sourcecode on Github

Leave a Comment

  1. private static void fizzBuzz(int i) {
    int printer = 0;
    for (int j = 0; printer 0)&&(j % 3 == 0) && (j % 5 == 0)) {
    System.out.println(“fizz buzz”);
    printer++;
    } else if ((j>0)&&(j % 3 == 0)) {
    System.out.println(“fizz”);
    printer++;
    } else if ((j>0)&&(j % 5 == 0)) {
    System.out.println(“buzz”);
    printer++;
    } else {
    System.out.println(j);
    }
    }
    System.out.println(“TRADITION”);
    }

    Reply
  2. List strRet = new ArrayList();

    for (int i=1; i<=n; i++)
    {
    if (i%15==0)
    strRet.add("FizzBuzz");

    // number divisible by 5, print 'Buzz'
    // in place of the number
    else if (i%5==0)
    strRet.add("Buzz");

    // number divisible by 3, print 'Fizz'
    // in place of the number
    else if (i%3==0)
    strRet.add("Fizz");

    // number divisible by 15(divisible by
    // both 3 & 5), print 'FizzBuzz' in
    // place of the number

    else // print the numbers
    strRet.add(Integer.toString(i));
    }

    return strRet; //return type "List”.
    }

    Reply
  3. Yet Another Way ;)

    package com.foo;
    
    import org.junit.Assert;
    import org.junit.Test;
    
    public class FizzBuzz {
    
    	private static final String FIZZ = "Fizz";
    	private static final String BUZZ = "Buzz";
    
    	@Test
    	public void test() {
    
    		loopFizzBuzz(1000000);
    
    		Assert.assertEquals(fizzBuzz(8), "8");
    		Assert.assertEquals(fizzBuzz(27), FIZZ);
    		Assert.assertEquals(fizzBuzz(80), BUZZ);
    		Assert.assertEquals(fizzBuzz(90), FIZZ + BUZZ);
    	}
    
    	private void loopFizzBuzz(int size) {
    
    		StringBuilder buffer = new StringBuilder();
    		final String lineSeparator = System.lineSeparator();
    
    		for (int i = 1; i <= size; i++) {
    
    			String chunk = fizzBuzz(i) + lineSeparator;
    			buffer.append(chunk);
    		}
    
    		System.out.println(buffer.toString());
    	}
    
    	private String fizzBuzz(int i) {
    
    		String label = String.valueOf(i);
    
    		boolean isFizz = i % 3 == 0;
    		boolean isBuzz = i % 5 == 0;
    
    		if (isFizz || isBuzz) {
    			label = "";
    		}
    		if (isFizz) {
    			label += FIZZ;
    		}
    		if (isBuzz) {
    			label += BUZZ;
    		}
    
    		return label;
    	}
    }
    
    Reply
  4. Do you think that the Java8 code example is more readable then the another one?
    It is indeed shortest, but not readable as the before java8 example, IMO.

    Reply

Leave a Comment

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.