How to Print Multiples of 5 in a Range in Python 🐍


Print Multiples of 5 in a Range in Python

In programming, it’s often useful to work with numbers in specific ranges and identify multiples of certain numbers. For example, finding multiples of 5 is a common task in number theory, mathematics, and real-life scenarios. In this blog, we will cover how to print the multiples of 5 in a specified range using Python.


What Are Multiples of 5?

A multiple of 5 is any number that can be divided evenly by 5. This means that when you divide a number by 5, the remainder is zero.

Examples of multiples of 5 include: 5, 10, 15, 20, 25, ....


Method 1: Using a For Loop

One of the simplest methods to find multiples of 5 in a given range is to use a for loop. We can iterate through each number in the range, check if it’s divisible by 5, and print it.

Code Example:

# Program to print multiples of 5 in a given range

# Step 1: Define the range
start = 1
end = 50

# Step 2: Use a for loop to iterate through the range
print(f"Multiples of 5 between {start} and {end}:")
for num in range(start, end + 1):
    if num % 5 == 0:
        print(num, end=" ")

Explanation:

  1. Range Definition: We define the start and end values for the range. In this case, we are looking for multiples of 5 between 1 and 50.
  2. For Loop: The for loop iterates through each number from start to end.
  3. Modulo Operation: Inside the loop, we check if the number is divisible by 5 using the modulo operator (%). If num % 5 == 0, it means the number is a multiple of 5.
  4. Output: The multiples of 5 are printed in a single line, separated by spaces.

Output:

Multiples of 5 between 1 and 50:
5 10 15 20 25 30 35 40 45 50

Method 2: Using a While Loop

We can achieve the same result using a while loop, which provides more flexibility if you want to control the loop with different conditions.

Code Example:

# Program to print multiples of 5 using a while loop

# Step 1: Define the range
start = 1
end = 50

# Step 2: Initialize the first multiple of 5 in the range
num = start

# Step 3: Use a while loop to find and print multiples of 5
print(f"Multiples of 5 between {start} and {end}:")
while num <= end:
    if num % 5 == 0:
        print(num, end=" ")
    num += 1

Explanation:

  1. Initialization: We start the loop from num = start and iterate until num reaches end.
  2. While Loop: The while loop continues as long as num is less than or equal to the end value.
  3. Modulo Operation: We check each number to see if it’s divisible by 5, similar to the for loop method.
  4. Increment: After each iteration, the value of num is incremented by 1 to check the next number.

Output:

Multiples of 5 between 1 and 50:
5 10 15 20 25 30 35 40 45 50

Method 3: Using List Comprehension

List comprehension is a concise and Pythonic way to generate lists based on certain conditions. We can use it to generate and print the multiples of 5 in a given range.

Code Example:

# Program to print multiples of 5 using list comprehension

# Step 1: Define the range
start = 1
end = 50

# Step 2: Use list comprehension to find multiples of 5
multiples_of_5 = [num for num in range(start, end + 1) if num % 5 == 0]

# Step 3: Print the result
print(f"Multiples of 5 between {start} and {end}: {multiples_of_5}")

Explanation:

  1. List Comprehension: We use list comprehension to create a list of numbers that are divisible by 5 in the specified range.
  2. Modulo Operation: The condition if num % 5 == 0 ensures that only multiples of 5 are included in the list.
  3. Output: The result is printed as a list of multiples of 5.

Output:

Multiples of 5 between 1 and 50: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

Method 4: Using a Step Argument in the Range Function

Another approach is to directly use the range() function’s step argument. Since we are only interested in multiples of 5, we can start from the first multiple of 5 and step by 5 to get the rest of the multiples.

Code Example:

# Program to print multiples of 5 using the range step argument

# Step 1: Define the range
start = 1
end = 50

# Step 2: Adjust start to the first multiple of 5
start = (start + 4) // 5 * 5  # Ensure 'start' is a multiple of 5

# Step 3: Use the step argument in range to print multiples of 5
print(f"Multiples of 5 between {start} and {end}:")
for num in range(start, end + 1, 5):
    print(num, end=" ")

Explanation:

  1. Range Adjustment: If start is not a multiple of 5, we adjust it using the expression (start + 4) // 5 * 5, which ensures we start at the first multiple of 5 greater than or equal to start.
  2. Step Argument: In the range() function, we specify a step of 5, so that Python automatically increments the numbers by 5.
  3. Output: The multiples of 5 are printed directly without checking each number.

Output:

Multiples of 5 between 5 and 50:
5 10 15 20 25 30 35 40 45 50

Conclusion

In this blog, we explored several methods to print multiples of 5 in a given range using Python:

  1. For loop: Simple and easy to understand.
  2. While loop: Provides more control over the loop execution.
  3. List comprehension: Concise and Pythonic.
  4. Range with step: Efficiently generates multiples directly.

Each method is useful in different scenarios, depending on the size of the dataset and the complexity of your task. Practice these techniques to improve your Python programming skills!


Stay tuned for more Python tutorials! 😊

Leave a Comment