How to Print Multiples of 5 in a Range Using Python

Finding multiples of a specific number within a range is a common task in programming, especially when working with loops and conditions. Python provides simple and efficient ways to accomplish this. In this blog, we’ll discuss various methods to print multiples of 5 within a given range.


What Are Multiples of 5?

A number is considered a multiple of 5 if it is divisible by 5 without any remainder. Mathematically: n is a multiple of 5 if n%5=0n \, \text{is a multiple of 5 if} \, n \% 5 = 0

For example:

  • 5, 10, 15, and 20 are multiples of 5.
  • Non-multiples include numbers like 3, 7, and 22.

Method 1: Using a For Loop

The most common way to find and print multiples of 5 is by iterating through a range and checking each number.

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 find multiples of 5
print(f"Multiples of 5 from {start} to {end} are:")
for number in range(start, end + 1):
    if number % 5 == 0:
        print(number, end=" ")

Explanation:

  1. The range(start, end + 1) generates numbers from start to end (inclusive).
  2. The condition number % 5 == 0 checks if the number is divisible by 5.
  3. Numbers that meet the condition are printed.

Output:

Multiples of 5 from 1 to 50 are:
5 10 15 20 25 30 35 40 45 50

Method 2: Using the range() Function with Step Size

If you know the range starts at a multiple of 5, you can use the range() function with a step size of 5 to generate multiples directly.

Code Example:

# Program to print multiples of 5 using range with step size

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

# Step 2: Use range with a step of 5
print(f"Multiples of 5 from {start} to {end} are:")
for number in range(start, end + 1, 5):
    print(number, end=" ")

Explanation:

  1. range(start, end + 1, 5) starts at start, increments by 5, and stops before end + 1.
  2. This approach is efficient as it avoids unnecessary checks.

Output:

Multiples of 5 from 5 to 50 are:
5 10 15 20 25 30 35 40 45 50

Method 3: Using a While Loop

A while loop gives more control over the iteration and can be used when the range or conditions are dynamic.

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 number
number = start

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

Explanation:

  1. The loop continues until number exceeds the end value.
  2. Each iteration checks if number % 5 == 0.

Output:

Multiples of 5 from 1 to 50 are:
5 10 15 20 25 30 35 40 45 50

Method 4: Using List Comprehension

List comprehension offers a concise way to generate and print multiples of 5.

Code Example:

# Program to print multiples of 5 using list comprehension

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

# Step 2: Generate multiples of 5 using list comprehension
multiples_of_5 = [number for number in range(start, end + 1) if number % 5 == 0]

# Step 3: Display the result
print(f"Multiples of 5 from {start} to {end} are:", multiples_of_5)

Explanation:

  • [number for number in range(start, end + 1) if number % 5 == 0] generates a list of multiples of 5 in the range.
  • The print() function displays the list.

Output:

Multiples of 5 from 1 to 50 are: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

Handling Edge Cases

  1. Empty Ranges:
    • If start > end, the range is empty, and no output is generated.
    • Example: range(50, 1) results in no multiples.
  2. Negative Ranges:
    • Multiples of 5 can be printed even if the range includes negative numbers.
    • Example:
    start = -50 end = -5 print([num for num in range(start, end + 1) if num % 5 == 0])

Conclusion

We explored several ways to print multiples of 5 in Python:

  1. Using a For Loop: Ideal for most scenarios.
  2. Using range() with Steps: Efficient for generating multiples directly.
  3. Using a While Loop: Useful for dynamic conditions.
  4. Using List Comprehension: Pythonic and concise.

Choose the method that suits your use case and enjoy coding in Python! 😊

Share with our team

Leave a Comment