write a program to print next prime number.

#WAY 1...
#wap to print next prime no. 
x=int(input("enter the no."))
iscontinue=True
while iscontinue:
    x=x+1
    print("checking if ",x," is a prime no.")
    for i in range(2,x):
        print("validating",i)
        if x%i==0:
            print(x,"is divisible by",i,"hence closing the loop")
            break
        elif i==x-1:
            print(x,"is a prime no. hence, closing the main while loop")
            iscontinue=False
print("therefore,",x ,"is the next prime no." )
    

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\next prime no. origional.py
enter the no.5
checking if  6  is a prime no.
validating 2
6 is divisible by 2 hence closing the loop
checking if  7  is a prime no.
validating 2
validating 3
validating 4
validating 5
validating 6
7 is a prime no. hence, closing the main while loop
therefore, 7 is the next prime no.
>>> 
#WAY 2... using function
#next prime using function
n=int(input("enter the no. "))
t=True
def nextprime(a):
    global n
    global t
    while t:
        n=n+1
        for i in range(2,n):
            if n%i==0:
                break
            else:
                t=False
    return(n)
run=nextprime(n)
print(run)  
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\next prime using function.py
enter the no. 55
57
>>> 

Leave a Reply

Back To Top