FizzBuzz Interview Question Python
FizzBuzz is one of the top Python interview questions asked to check, How the candidate arrives at the logic for solving this problem.
Instruction:
This program should print the solution to the Fizz Buzz game automatically once the python program is executed
- In this game, Our program should print from 1 -> 100
- Whenever there is a number divisible by 3, then instead of printing the number, it should print “Fizz”
- Whenever there is a number divisible by 5, then instead of printing the number, it should print “Buzz”
- At Last, It also checks, if the number is divisible by both 3 and 5, if yes then instead of the number it should print “FizzBuzz”
To create this program, we need to be aware of the below topics
- Loop.
- Condition flow
- Modulo
Modulo
Modulo is denoted by the symbol “%“, and it returns the remainder of the number
For example:
11 % 4 => 3
10 % 2 => 0
To know about Loop and condition flow, Please check here
Code:
for number in range(1,101): if number % 3 ==0 and number % 5 ==0: print(“FizzBuzz”) elif number % 3 ==0: print(“Fizz”) elif number % 5 ==0: print(“Buzz”) else: print(number)
Output:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
Yes, that’s it
— Basically, we are looping through from 1 -> 101
— having 3 conditions to check divisible by 3,5 and both else just print the number directly