program to add all prime numbers in a given range using list.

WAY 1...
#program to add all prime no.in a given range in a list-
x=int(input("enter the starting range "))
y=int(input("enter the ending range "))
prime=[]
for i in range(x,y+1):
        fact=0
        for a in range(2,i):
            if i%a==0:
                fact=1
                break
        if fact==0:
            prime.append(i)
print("list of prime no. is",prime)
    
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\program to add all prime no.ina given range in a list.py
enter the starting range 11
enter the ending range 20
list of prime no. is [11, 13, 17, 19]
>>> 
WAY 2...using function
#wap to append all the prime no. in a list in a given range using
#function
start=int(input("enter the starting range "))
end=int(input("enter the ending range "))
prime=[]
def primelist(a,b):
    for i in range(start,end+1):
        fact=0
        for j in range(2,i):
            if i%j==0:
                fact=1
                break
        if fact==0:
            prime.append(i)
    return(prime)
p=primelist(start,end)
print(p)
                       
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:\Users\Dell\AppData\Local\Programs\Python\Python38-32\append all the prime no. in a list in a given range using function.py
enter the starting range 11
enter the ending range 20
[11, 13, 17, 19]
>>> 

Leave a Reply

Back To Top