Fizz-Buzz Program in Python

Fizz-Buzz is a simple and popular programming challenge used to teach logic and control flow in coding.

Problem Description:

Write a program to print numbers from 1 to a given number, but:

  • If the number is a multiple of 3, print "Fizz" instead of the number.
  • If the number is a multiple of 5, print "Buzz" instead of the number.
  • For numbers which are multiples of both 3 and 5, print "FizzBuzz".
  • Otherwise just print out the number.

Step-by-Step Solution:

1. Understand the Problem

We want:

  • To iterate through numbers in a range.
  • Determine whether a number is divisible by 3, 5, or both, using the modulus operator %.
    • % returns the remainder after division. For instance,
      • 6 % 3 == 0 means that 6 is divisible by 3.
      • 10 % 5 == 0 implies that 10 is divisible by 5.
  • Print the correct output according to the conditions.

2. Write the Algorithm

  • Start with a loop that runs from 1 to the maximum number.
  • Check if the current number is divisible by 3 and 5 (both).
  • If not, check if it is divisible by 3.
  • If not, check if it is divisible by 5.
  • If none of the above, print the number.

3. Write the Code

Here’s the Python code:

# Define the maximum number
max_number = 20  # You can change this to any number

# Loop through numbers from 1 to max_number
for num in range(1, max_number + 1):  # range(1, 21) means numbers 1 to 20
    # Check if divisible by both 3 and 5
    if num % 3 == 0 and num % 5 == 0:
        print("FizzBuzz")
    # Check if divisible by 3
    elif num % 3 == 0:
        print("Fizz")
    # Check if divisible by 5
    elif num % 5 == 0:
        print("Buzz")
    # If none of the above, print the number
    else:
        print(num)

Explanation of Each Part:

1. max_number = 20

  • This sets the range of numbers to evaluate (1 to 20 in this case).
  • You can replace 20 with any number to change the range.

2. for num in range(1, max_number + 1):

  • range(1, max_number + 1) generates numbers from 1 to max_number (inclusive).
  • The loop runs once for each number in the range.

3. if num % 3 == 0 and num % 5 == 0:

  • It checks whether the number is divisible by both 3 and 5.
  • num % 3 == 0 is True if num is divisible by 3. Remainder is 0.
  • num % 5 == 0 is True if num is divisible by 5. Remainder is 0.
  • If this is both True, then print "FizzBuzz".

4. elif num % 3 == 0:

  • If the first condition is False, then check whether the number is divisible by 3.
  • If it is true, print "Fizz".

5. elif num % 5 == 0:

  • If the above conditions are false, check if the number is divisible by 5.
  • If it is true, print "Buzz".

6. else:

  • If none of the conditions are met, print the number itself.

Sample Output:

For max_number = 20, the output will be:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

How to Experiment:

  • Change max_number to test with larger ranges.
  • Add more conditions, like checking for divisibility by 7 (num % 7 == 0), to enhance the program.

Key Python Ideas Used:

  1. Loops (for): Used to iterate over a range of numbers.
  2. Conditional Statements (if, elif, else): The conditional statements check conditions and run the code on them.
  3. Modulus Operator (%): This operator calculates the remainder or checks for divisibility.
  4. Printing (print()): Outputs the result.