Programming Examples
Python program to check whether it is a prime number or not If it is not a prime then display the next number that is prime
Write a Python code to input a number and check whether it is a prime number or not. If it is not a prime then display the next number that is prime.
Sample Input: 14
Sample Output: 17
Solution:
n=int(input("Enter a number: "))
c=c1=m=0
for a in range(1, n+1):
if(n%a==0):
c=c+1
if(c==2):
print(n, "is a prime number")
else:
a=n
m=n
while(c1!=2):
a=a+1
c1=0
for p in range(1,a+1):
if(a%p==0):
c1=c1+1
print("Next prime number to ", m , " is ",a)
Output
Enter a number: 11
11 is a prime number
>>>
==========================
Enter a number: 24
Next prime number to 24 is 29