# Solving Fizz-Buzz Problem

Ram Bhardwajverified

@Sidharth

Founder and CEO at SidTube. Instagram: https://sidtu.be/r/instagram Discord: https://sidtu.be/r/MyDiscordServer

FizzBuzz Problem

Given a positive integer A, return an array of strings with all the integers from 1 to N.
But for multiples of 3, the array should have “Fizz” instead of the number.
For the multiples of 5, the array should have “Buzz” instead of the number.
For numbers that are multiples of 3 and 5 both, the array should have “FizzBuzz” instead of the number.

Solution:

pseudocode-

`n = INPUT NUMBER`

`PRINTING NUMBERS FROM 1 TO N:`

`       IF NUMBER IS DIVISIBLE BY 3 AND 5`

`             THEN PRINT FIZZBUZZ`

`       ELSE IF NUMBER IS DIVISIBLE BY 3`

`             THEN PRINT FIZZ`

`       ELSE IF NUMBER IS DIVISIBLE BY 5`

`             THEN PRINT BUZZ`

Source Code-

First, ask for a number so that we will print the numbers from 1 to that number

``````#ask for number
n = input("enter a number: ")

#printing numbers from 1 to that number
for num in range(1, n+1):
print(num)``````

output

``````enter a number: 5
1
2
3
4
5
[Finished in 0.1s]``````

Now here comes the core of the program, we have to print numbers based on if they are divisible by 3, 5 or both

so here we go

``````for num in range(1, 101):
if num%3 == 0 and num%5 == 0:
# if number is divisible by both 3 and 5
print("fizzbuzz")
elif num%3 == 0:
# if number is divisible by 3
print("fizz")
elif num%5 == 0:
# if number is divisible by 5
print("buzz")
else:
# otherwise just print the number
print(num)``````

full source code-

``````# fizz-buzz

n = input("enter a number: ")

#printing numbers from 1 to that number
for num in range(1, 101):
if num%3 == 0 and num%5 == 0:
# if number is divisible by both 3 and 5
print("fizzbuzz")
elif num%3 == 0:
# if number is divisible by 3
print("fizz")
elif num%5 == 0:
# if number is divisible by 5
print("buzz")
else:
# otherwise just print the number
print(num)``````

output-

``````enter a number: 100
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
fizz
22
23
fizz
buzz
26
fizz
28
29
fizzbuzz
31
32
fizz
34
buzz
fizz
37
38
fizz
buzz
41
fizz
43
44
fizzbuzz
46
47
fizz
49
buzz
fizz
52
53
fizz
buzz
56
fizz
58
59
fizzbuzz
61
62
fizz
64
buzz
fizz
67
68
fizz
buzz
71
fizz
73
74
fizzbuzz
76
77
fizz
79
buzz
fizz
82
83
fizz
buzz
86
fizz
88
89
fizzbuzz
91
92
fizz
94
buzz
fizz
97
98
fizz
buzz
[Finished in 0.1s]``````

So, now our program is working and we've successfully solved the problem!

Related Articles