Course of Raku / Essentials / Conditional checks / Exercises / FizzBuzz

Solution: FizzBuzz

This task is a classical interview task that helps to check if you think of the cases when both conditions are True. If the number is divisible by both 3 and 5, the program should print FizzBuzz.

Code

Here is the solution:

my $n = prompt 'Enter a number: ';

print 'Fizz' if $n %% 3;
print 'Buzz' if $n %% 5;
print "\n";

🦋 Find the program in the file fizz-buzz.raku.

Example

You need to test a few classes of input data:

  • The numbers divisible by 3, e.g.: 3, 6, 9, 12.
  • The numbers divisible by 5, e.g.: 5, 10, 15, 20.
  • The numbers divisible by 3 and by 5, e.g.: 15, 30, 45.
  • Other numbers, which are divisible by neither 3 nor 5, e.g.: 4, 7, 11.
$ raku exercises/conditional-checks/fizz-buzz.raku 
Enter a number: 3
Fizz

$ raku exercises/conditional-checks/fizz-buzz.raku
Enter a number: 10
Buzz

$ raku exercises/conditional-checks/fizz-buzz.raku
Enter a number: 7

$ raku exercises/conditional-checks/fizz-buzz.raku
Enter a number: 30
FizzBuzz

Next exercise

💪 How many numbers are equal?

Course navigation

Code blocks / Local variables   |   Loops