Python
Python

 

The FizzBuzz problem is a well-known coding exercise often used to evaluate basic programming skills. The task is to print the numbers from 1 to 100, but with the following rules:

  • For multiples of 3, print “Fizz” instead of the number.
  • For multiples of 5, print “Buzz” instead of the number.
  • For multiples of both 3 and 5, print “FizzBuzz”.

This problem helps demonstrate an understanding of loops, conditionals, and modular arithmetic. The solution will involve iterating through numbers, applying the given rules, and printing the appropriate result for each number.

Objective

The goal is to implement a Python program that:

  • Iterates through numbers 1 to 100.
  • Prints “Fizz” for multiples of 3.
  • Prints “Buzz” for multiples of 5.
  • Prints “FizzBuzz” for numbers that are multiples of both 3 and 5.
  • Prints the number itself if it is neither a multiple of 3 nor 5.

Python Code for FizzBuzz


# FizzBuzz Program in Python

# Loop through numbers from 1 to 100
for num in range(1, 101):
    # Check if the number is divisible by both 3 and 5
    if num % 3 == 0 and num % 5 == 0:
        print("FizzBuzz")
    # Check if the number is divisible by 3
    elif num % 3 == 0:
        print("Fizz")
    # Check if the number is divisible by 5
    elif num % 5 == 0:
        print("Buzz")
    # If the number is neither divisible by 3 nor 5, print the number
    else:
        print(num)
        

Explanation of the Program Structure

The program is structured in a simple loop that iterates over the range of numbers from 1 to 100. The key component of the logic is the use of conditional statements to check divisibility using the modulus operator (%).

Breakdown of the Code:

  • for num in range(1, 101):
    This initializes a loop that runs through the numbers 1 to 100.
  • if num % 3 == 0 and num % 5 == 0:
    This condition checks whether the current number is divisible by both 3 and 5 (i.e., a multiple of 15). If true, it prints “FizzBuzz”.
  • elif num % 3 == 0:
    If the first condition is false, this checks whether the number is divisible by 3, and prints “Fizz” if true.
  • elif num % 5 == 0:
    Similarly, if neither of the previous conditions is true, this checks for divisibility by 5 and prints “Buzz” if the condition is satisfied.
  • else:
    If none of the conditions are met, the number itself is printed.

How to Run the Program

To run this Python program, follow these steps:

    1. Open a text editor and copy the code into a new file.
    2. Save the file with a .py extension, for example, fizzbuzz.py.
    3. Ensure that you have Python installed on your system (Python 3.x is recommended).
    4. Open a terminal or command prompt window.
    5. Navigate to the directory where you saved fizzbuzz.py.
    6. Run the program by typing the following command and pressing Enter:
python fizzbuzz.py
  1. You should now see the numbers from 1 to 100 printed on the screen with “Fizz”, “Buzz”, and “FizzBuzz” as described in the problem statement.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)