Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range


A multiple of a number n is also divisible by the same number n. If a number M is a multiple of a number n, then if we divide M by n, its remainder must be zero. Also to create the multiples of number n within a given range, we can add n multiple times and check that the resulting number is within the given range or not. Another way to make multiples of a given number n, within a given range, is to find the first multiple in the given range for n and then to find the value of the number q, which was multiplied with n, to produce that multiple. Then keep increasing the q with 1 and keep multiplying n by it, till we get all multiples of n within a given range.

Example 1 - Find Numbers Divisible by 7 and Multiple of 5 in a Given Range by using the remainder equal to zero method.

Algorithm

Step 1 − Specify the smallest and highest number in the range.

Step 2 − Select the number from this range if that number when divided by 7 will have remainder zero. Print this number.

Step 3 − Select the number from this range if that number when divided by 5 will have remainder zero. Print this number.

Step 4 − Also print those numbers which follow both the rules given in Step 2 and Step 3

Step 5 − Run the program and then check the result.

The Python File Contains this

lowNum=50
highNum=100

print("\nIn the given range from ", lowNum, " to", highNum, " :")
for item in range(lowNum, highNum):
   if (item % 7 == 0):
      print("This number ", item, "is divisible by 7")

print("\nIn the given range from ", lowNum, " to", highNum, " :")
for item in range(lowNum, highNum):
   if (item % 5 == 0):
      print("This number ", item, " is multiple of 5")        

print("\nIn the given range from ", lowNum, " to", highNum, " :")
for item in range(lowNum, highNum):
   if (item % 7 == 0) and (item % 5 == 0):
      print("This number ", item, "is divisible by 7 and also a multiple of 5")

Viewing The Result - Example 1

For seeing the result run the Python file in the cmd window.

In the given range from  50  to 100  :
This number  56 is divisible by 7
This number  63 is divisible by 7
This number  70 is divisible by 7
This number  77 is divisible by 7
This number  84 is divisible by 7
This number  91 is divisible by 7
This number  98 is divisible by 7

In the given range from  50  to 100  :
This number  50  is multiple of 5
This number  55  is multiple of 5
This number  60  is multiple of 5
This number  65  is multiple of 5
This number  70  is multiple of 5
This number  75  is multiple of 5
This number  80  is multiple of 5
This number  85  is multiple of 5
This number  90  is multiple of 5
This number  95  is multiple of 5

In the given range from  50  to 100  :
This number  70 is divisible by 7 and also a multiple of 5

Fig 1: Showing the result in the command window.

Example 2: Find Numbers Divisible by 7 and Multiple of 5 in a Given Range by using adding method.

Algorithm

Step 1 − Specify the smallest and highest number in the range.

Step 2 − First identify the smallest number within the range that is divisible by 7. Keep adding 7 to it again and again till you find the highest such number in this range. Print these numbers.

Step 3 − First find the smallest number within the range that is a multiple of 5. Keep adding 5 to it again and again till you find the highest such number in this range. Print these numbers

Step 4 − Also print those numbers which follow both the rules given in Step 2 and Step 3, by finding the common elements in both lists.

Step 5 − Execute the program and then verify the result.

The Python File Contains this

list1=[]
list2=[]

lowNum=200
highNum=300

tobeadded=7 - (lowNum % 7)
startNum= lowNum + tobeadded

for item in range(lowNum, highNum):
   if item==startNum:
      list1.append(startNum)
      startNum += 7

tobeadded1= 5 - (lowNum % 5)
startNum1= lowNum + tobeadded1

for item in range(lowNum, highNum):
   if item==startNum1:
      list2.append(startNum1)
      startNum1 += 5        

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe following numbers are divisible by 7 :")
print(list1)

print("\nThe following numbers are multiple of 5 :")
print(list2)
    
inbothlists = [ele for ele in list1 if ele in list2]
print("\nIn the given range from ", lowNum, " to", highNum, " :")
print("These numbers ", inbothlists, "are divisible by 7 and also a multiple of 5")

Viewing The Result - Example 2

Open the cmd window and run the python file to see the result.

D:\articles\pythonarticles\rangedivby7mulby5>py main1.py
In the given range from  200  to 300  :

The following numbers are divisible by 7 :
[203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294]

The following numbers are multiple of 5 :
[205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295]

In the given range from  200  to 300  :
These numbers  [210, 245, 280] are divisible by 7 and also a multiple of 5

Fig 2: Showing the result in form of lists.

Example 3: Find Numbers Divisible by 7 and Multiple of 5 in a Given Range by using the multiplication method.

Algorithm

Step 1 − Set the smallest and highest number in the range.

Step 2 − First, identify the smallest number within the range that is divisible by 7. If it is the qth multiple of 7, keep increasing q by 1 and keep finding 7*q again and again till you find the highest such number in this range. Print these numbers.

Step 3 − And then find the smallest number within the range that is a multiple of 5. If it is the qth multiple of 5, keep increasing q by 1 and keep finding 5*q again and again till you find the highest such number in this range. Print these numbers.

Step 4 − Also print those numbers which follow both the rules given in Step 2 and Step 3, by finding the common elements in both lists.

Step 5 − Run the program and then check the result

The Python File Contains this

list1=[]
list2=[]
lowNum=200
highNum=300

tobeadded=7 - (lowNum % 7)
startNum= lowNum + tobeadded
numoftimes=int(startNum/7)
print(numoftimes)

for item in range(lowNum, highNum):
   if item==startNum:
      list1.append(startNum)
      numoftimes += 1
      startNum = 7 * (numoftimes)

tobeadded1=5 - (lowNum % 5)
startNum1= lowNum + tobeadded1
numoftimess=int(startNum1/5)
print(numoftimess)

for item in range(lowNum, highNum):
   if item==startNum1:
      list2.append(startNum1)
      numoftimess += 1
      startNum1 = 5 * numoftimess    

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe following numbers are divisible by 7 :")
print(list1)

print("\nThe following numbers are multiple of 5 :")
print(list2)

inbothlists = [ele for ele in list1 if ele in list2]
print("\nIn the given range from ", lowNum, " to", highNum, " :")
print("These numbers ", inbothlists, "are divisible by 7 and also a multiple of 5")

Viewing The Result - Example 3

Open the cmd window and run the python file to see the result.

29
41
In the given range from  200  to 300  :

The following numbers are divisible by 7 :
[203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294]

The following numbers are multiple of 5 :
[205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295]

In the given range from  200  to 300  :
These numbers  [210, 245, 280] are divisible by 7 and also a multiple of 5

Fig 3: Showing the result in form of lists.

Conclusion

In this Python article, using three different examples, the ways to show how to find all numbers within a given range, that are divisible by 7 and those that are multiples of 5 are given. Using the three methods, namely using the find the remainder equal to zero methods, adding the number multiple times, and also multiplying the number n by the number q, to produce the multiples of a given number n in a range are specified.

Updated on: 10-Jul-2023

795 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements