Write a program to check if a number is prime or not. and also print all Prime Numbers in given Interval by user.

Q. Write a program to 
    (i) Check if a number is prime or not.
   (ii) Print all Prime Numbers in given Interval by user.

Code:-
(i)

def primecheck(n):
  if n > 1:
    for i in range(2,int(n/2)+1):
      if (n % i) == 0:
        print(n,"is not a prime number.")
        break
    else:
      print(n,"is a prime number.")
  else:
    print(n,"is not a prime number.")

n = int(input("Enter a number:"))
primecheck(n)

Output:-

Code:-
(ii)
lower = int(input("Enter lower range:"))
upper = int(input("Enter upper range:"))

for num in range(lower, upper + 1):
  if num > 1:
    for i in range(2, num):
      if (num % i) == 0:
        break
    else:
      print(num)

Output:-





Comments