PYTHON PROGRAM TO PRINT ALL PRIME NUMBERS BETWEEN Start TO Ending number
Prime numbers between start to ending
1.To understand this algorithm ou have to know that what is prime number
Definition:Number which is divisible by 1 and itself called prime number.
It means prime number only completely divide only 2 times.
2.Now take a input of n th term or last number so that program goes upto 1 to N.
3.Take a count variable is 0. To count number of times
4.Take a for loop to go upto last number i.e N (ending number).
5.Divide every number from first loop to the second loop which will go upto ( 1 to that number )
6.If count==2:(testing if that number is divide only two times ( from 1 to itself )
then use print() function to print that number
#To understand this program read the steps given above:
>>>Copy the code by clicking on it
"""
WAP to print prime number between start to ending numbers
Let understand
start=1
ending=10
the value i will go upto ending number i.e 1 to 10
now another loop i.e value j will go upto 1 to i
it means
Loop 1:
first value of i =1
i%j=1%1=0
count=1
second time loop
1%2!=0
so count=1 remains same ,so it is not prime .
"""
#let understand and intialise and program and use input() to input the first number i.e starting number
start=int(input("Enter a first number:"))
#Input last number using input() function
ending=int(input("Enter a last number:"))
#take count variable is 0 (count means no. of times divide )
count=0
#Now take a loop to iterate which goes upto first number to last no.
for i in range(start,ending+1):
#Now another loop to test every no. of i which divide by the value of j(1 to i ) i means value of i
for j in range(1,i+1):
if i%j==0: #here testing if value i is completely divisible by j (modulus operator give always decimal part)
#Then count no. of times remanider will be 0
count=count+1
#After the completition of one number test if it is divided only 2 times then use print() function to print that number
if count==2:
print("Prime numbers between",start,"to",ending, "is:",i)
#After testing one number again value of count will be 0
count=0
Testcases:1
Input:
start=1
ending=10
Output:
2 3 5 7
Testcases:2
Input:
start=20
ending=70
Output:
23
29
31
37
41
43
47
53
59
61
67
#What ever is written line using # sign i.e comment you only have undersatnd
No comments: