Python program to print all the numbers divisible by 3 and 5 for a given number

PythonServer Side ProgrammingProgramming

This is a python program to print all the numbers which are divisible by 3 and 5 from a given interger N. There are numerous ways we can write this program except that we need to check if the number is fully divisble by both 3 and 5.

Below is my code to write a python program to print all the numbers divisible by 3 and 5 −

lower = int(input("Enter lower range limit:"))
upper = int(input("Enter upper range limit:"))
for i in range(lower, upper+1):
   if((i%3==0) & (i%5==0)):
      print(i)

Output

Enter lower range limit:0
Enter upper range limit:99
0
15
30
45
60
75
90

Above we try to print all the numbers between 0 and 99 which are divisble by 3 and 5. Same program can be used to print all the number between 0 and 1000 which are divisible by 3 and 5, we just need to alter our range and our output will be something like,

Enter lower range limit:0
Enter upper range limit:1000
0
15
30
45
60
75
90
105
120
135
150
165
180
195
....
....
915
930
945
960
975
990

In case we want to write a program which will print all the numbers in a range divisible by a given number not the fixed number like above, i just need to update by program like,

#Incase we want to print all number between a range divided by any given number
n = int(input("Enter the number to be divided by:"))
for i in range(lower, upper+1):
   if(i%n==0):
      print(i)

Below the steps to write above code −

  • Take the lower and upper limit .i.e. the range from the user.

  • Take the number to be divided by from the user. In case of our main problem, because we know that numbers(3 and 5), i write the 3 and 5 in the if statement only.

  • Using a loop with &(and) operator statement(so that it print only those numbers which are divisble by both 3 & 5), prints all the factors which is divisible by the number.

  • Exit.

raja
Published on 19-Feb-2019 16:25:42
Advertisements