FizzBuzzer Advanced

Collapse Content

In the last challenge, you printed 100 lines and changed the numbers every 3 or 5 times. In this challenge, you will be given three integers n, a and b as input which will tell you how many lines to print and when to print Fizz or Buzz.

Write a program that prints the numbers from 1 through n. But for multiples of a print “Fizz” instead of the number and for the multiples of b print “Buzz”. For numbers which are multiples of both a and b print “FizzBuzz”.

Note: Print each item space-separated, and each test case on its own line.

Input Format
Boilerplate code is provided that passes in each input case into 3 variables: n, a and b.

If you want to process the input yourself, it uses the following format:

  • The input starts with T, the number of cases.
  • T lines follow, which each contains 3 integers in order for n, a and b.


Print the numbers from 1..n (space-separated) except for every a and b times, when you should print "Fizz" and "Buzz".

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.


  • I'm not sure what to do with the "Other Input" part. Can get everything else to work but don't know what is expected for "Other Input".

  • @Kirsten, it now shows all 4 cases.

  • Great, thanks!! That was a super quick fix : )

  • I think there could be a problem with this exercise. It was working fine on the previous page with the hard-coded numbers, but since changing these to the variables n, a and b for this advanced

  • @Victoria, in this challenge you need to print each word space-separated and only add new lines after each test case. See the "Correct Output" shown above.

  • Thanks, that worked. :)

  • What I did is, make a string and assign values to it inside if statements in a for loop and if a certain condition was met than the string was changed to Fizz or Buzz and printed out, else I printed the number i which is <= n

    Hope it helps. :P

  • How shoud I define a,b and n?

  • @saeed, I added a note about the input format.

  •     Scanner in = new Scanner(;
        int loops = in.nextInt();
        for (int i = 0; i < loops; i++) {
            int max = in.nextInt();
  • my code
    Hey, guys. I got through the last challenge no problem. I also know how to use the Scanner for input, but it doesn't seem to want to run my code based on

  • You can use the provided boilerplate code to take in input, you don't need your own scanner. Note that each case consists of 3 numbers, n, a and b. Print out n items for each case.

  • no featured answer? I know my code sucks, so I'd like to see how it could be done better.

  • Featured answer added.

  • Excellent.

  • Confused about declaring n, a, and b. Normally in these challenges these are already declared, but I don't see it in this code. Here is the code I'm using which is which throws a compilation error, even though the same code worked on the previous challenge when n = 100, a = 3, and b = 5:

  • You can't just compare to n without having declared and assigned it. You need to declare the variables yourself (or use actual numbers.)

  • I'm expecting t to be an int....
    "The input starts with T, the number of cases."

    but it compile errors because it says T is a string!

  • Since this challenge involves standard input, you may want to use the boilerplate code to process the input for you.

Contact Us
Sign in or email us at [email protected]