Write a program to find whether the given number is prime or not


This program takes an input from the user as a number and then using a for loop and if-else statement it checks if the number is a prime number or not. The for loop iterates from 1 to the input number and checks if the input number is divisible by each number in the range. If it is divisible, the sum variable is incremented by 1. After the loop ends, the if-else statement checks if the value of sum is equal to 2. If it is, it prints that the input number is a prime number, otherwise it prints that it's not a prime number. The program uses the concept of divisibility, looping and conditional statements.


Source Code

"""num = int(input("Enter the Number :"))
if num > 1:
 
	for i in range(2, num+1):
		if (num % i) == 0:
			print(num, "is not a prime number")
			break
	else:
		print(num, "is a prime number")
 
else:
	print(num, "is not a prime number")
 
"""
x = int(input("Enter a number: "))
sum=0
for i in range(1, x + 1):
       if x % i == 0:
           sum=sum+1
if(sum==2):
	print("This is a Prime Number")
else:
	print("This is a Not Prime Number")

Output

Enter a number: 23
This is a Prime Number

Example Programs