FizzBuzz Solution in Java

FizzBuzz is a fun game mostly played in elementary school. The rules are simple: when your turn arrives, you say the next number. However, if that number is a multiple of five, you should say the word “fizz” (preferably with a French accent) instead. If the number is a multiple of seven, you should say “buzz.” And if it is a multiple of both, you should say “fizzbuzz.” If you mess up, you’re out, and the game continues without you.

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.

Solve FizzBuzz in Java 8

With latest java version being 8, lets design a solution using java 8 constructs.

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

Solve FizzBuzz before Java 8

If you are still not using java 8, then this fizzbuzz solution uses basic for-loop and iterate over range of numbers and decide what to print.

for (int i = 1; i <= num; i++) 
{
	if (((i % 5) == 0) && ((i % 7) == 0)) // Is it a multiple of 5 & 7?
		System.out.println("fizzbuzz");
	else if ((i % 5) == 0) // Is it a multiple of 5?
		System.out.println("fizz");
	else if ((i % 7) == 0) // Is it a multiple of 7?
		System.out.println("buzz");
	else
		System.out.println(i); // Not a multiple of 5 or 7
}

Complete FizzBuzz Example Code

Both solutions are easy enough and self explanatory. Given below is working example sourcecode for fizzbuzz. Feel free to modify and play with the code.

import java.util.stream.IntStream;

public class FizzBuzz 
{
	public static void main(String[] args) 
	{
		fizzBuzzBeforeJava8(100);
		fizzBuzzInJava8(100);
	}

	private static void fizzBuzzBeforeJava8(int num) 
	{
		for (int i = 1; i <= num; i++) 
		{
			if (((i % 5) == 0) && ((i % 7) == 0)) // Is it a multiple of 5 & 7?
				System.out.println("fizzbuzz");
			else if ((i % 5) == 0) // Is it a multiple of 5?
				System.out.println("fizz");
			else if ((i % 7) == 0) // Is it a multiple of 7?
				System.out.println("buzz");
			else
				System.out.println(i); // Not a multiple of 5 or 7
		}
	}

	private static void fizzBuzzInJava8(int num) {
		IntStream.rangeClosed(1, 100)
				.mapToObj(i -> i % 5 == 0 ? (i % 7 == 0 ? "FizzBuzz" : "Fizz") : (i % 7 == 0 ? "Buzz" : i))
				.forEach(System.out::println);
	}
}

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

5 thoughts on “FizzBuzz Solution in Java”

  1. 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
  2. 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
  3. 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

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.