Python
Python

 

Introduction

In this program, we will explore how to print the numbers from 1 to 100 using Python,
but with some special rules. For example, if the number is divisible by 3, we will print
“Fizz”, if divisible by 5, we will print “Buzz”, and if divisible by both 3 and 5,
we will print “FizzBuzz”. This type of problem is commonly known as the “FizzBuzz” problem.

Objective

The goal of this program is to:

  • Print numbers from 1 to 100.
  • Apply special conditions for divisibility by 3, 5, or both.
  • Learn basic loops and conditional statements in Python.

Python Code


# Python program to print numbers from 1 to 100 with special rules

# 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")
    else:
        # If the number is not divisible by 3 or 5, print the number itself
        print(num)

Explanation of the Program

This Python program follows these steps:

  1. We use a for loop to iterate over the numbers from 1 to 100. The range function range(1, 101) generates numbers from 1 to 100.
  2. Inside the loop, we first check if the number is divisible by both 3 and 5 using the modulo operator (%). If the remainder of dividing the number by 3 and 5 is 0, we print “FizzBuzz”.
  3. If the number is divisible only by 3, we print “Fizz”. This is checked using the condition num % 3 == 0.
  4. If the number is divisible only by 5, we print “Buzz”, checked with num % 5 == 0.
  5. If the number is divisible by neither 3 nor 5, we simply print the number itself.

The if, elif, and else statements help in executing different code blocks based on the conditions.

How to Run the Program

To run this program, you need to follow these steps:

  1. Install Python on your machine if you haven’t already. You can download it from here.
  2. Open any text editor or an IDE (like PyCharm, VS Code, or IDLE).
  3. Create a new Python file (e.g., fizzbuzz.py) and paste the code above into the file.
  4. Save the file.
  5. Open the command line or terminal, navigate to the folder where the file is saved, and run the program using the command python fizzbuzz.py.
  6. The program will display numbers from 1 to 100, following the rules explained.
© 2024 Learn Programming. All rights reserved.

 

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 :)