175k views
5 votes
When given a number N, for each integer i in the range from 1 to N inclusive, print one value per line as follows:

-if i is a multiple of both 3 and 5, print FizzBuzz
-if i is a multiple of 3 but not 5, print Fizz
-if i is a multiple of 5 but not 3, print Buzz
-if i is not a multiple of 3 or 5, print value of i.

User Mkko
by
7.1k points

1 Answer

3 votes

Final answer:

To solve this problem, you can use a series of if-else statements to check if a number is divisible by 3 or 5 and print the corresponding output.

Step-by-step explanation:

To solve this problem, we will go through each integer i in the range from 1 to N and check its divisibility by 3 and 5 using the modulo operator (%) in a series of if-else statements.

If i is divisible by both 3 and 5, we will print 'FizzBuzz'. If i is only divisible by 3, we will print 'Fizz'. If i is only divisible by 5, we will print 'Buzz'. And if i is not divisible by 3 or 5, we will print the value of i.

Here's an example solution in Python:

N = 15
for i in range(1, N+1):
if i % 3 == 0 and i % 5 == 0:
print('FizzBuzz')
elif i % 3 == 0:
print('Fizz')
elif i % 5 == 0:
print('Buzz')
else:
print(i)

User Luiz Martins
by
8.0k points