FizzBuzzer Advanced
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
andb
.
Challenge
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.
Comments
Kirsten Smith
Jan 26, 1:19 PMI'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".
Learneroo
Jan 26, 2:33 PM@Kirsten, it now shows all 4 cases.
Kirsten Smith
Jan 26, 2:41 PMGreat, thanks!! That was a super quick fix : )
Victoria Holland
Jan 27, 12:03 PMI 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
exercise, it says the output is incorrect for all 4 cases. However, I tested my code in another compiler (using the same input cases) and the output was fine.
Learneroo
Jan 27, 12:07 PM@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.
Victoria Holland
Jan 27, 3:05 PMThanks, that worked. :)
Dino Šišić
Feb 28, 3:21 AMWhat 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
saeed
Mar 31, 3:24 AMHow shoud I define a,b and n?
Learneroo
Mar 31, 10:55 AM@saeed, I added a note about the input format.
Viktor Ayadi
Jul 27, 9:48 AMmy 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
the inputs that I'm taking. The syntax for the loops should be the same, so I'm not sure where I'm getting hung up. I am using print() so that everything prints on the same line, until the end where I use println() to end the test case.
Learneroo
May 18, 9:44 PMYou 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
andb
. Print outn
items for each case.Bolke
Sep 24, 6:25 PMno featured answer? I know my code sucks, so I'd like to see how it could be done better.
Learneroo
Sep 24, 7:11 PMFeatured answer added.
Bolke
Sep 25, 7:44 AMExcellent.
Gary
Mar 3, 5:15 PMConfused 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:
my code
Learneroo
Mar 3, 8:31 PMYou can't just compare to
n
without having declared and assigned it. You need to declare the variables yourself (or use actual numbers.)catypus
Jun 21, 5:45 AMI'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!
Learneroo
Jun 21, 8:00 PMSince this challenge involves standard input, you may want to use the boilerplate code to process the input for you.