In this python if else program, we will check whether an number is a prime number or not.
A prime number is a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1.
Hint:
Use for loop with the range function and use if statement inside the loop
Steps to solve the program
- Take a number as input through the user and create a variable count and assign its value equal to 0.
- Use for loop with the range function to iterate over 2 to the given number.
- During the iteration check if any number divided the given number then add 1 to the count variable using if-else statement.
- If value of the count variable is greater then 0 then it is not a prime number.
- Print the respective output.
# Take input through the user
num = int(input("Enter a number: "))
# Create count variable
count = 0
# Iterate over numbers
for i in range(2,num):
# Check for division
if num%i == 0:
# Add 1 to the count variable
count += 1
# Check for prime number
if count > 0:
# Print output
print("It is not a prime number")
else:
#Print output
print("It is a prime number")
Output :
Enter a number: 59
It is a prime number
Related Articles
Python program to check given number is odd or even.
Python program to check a given number is part of the Fibonacci series from 1 to 10.
Python program to check authentication with the given username and password.
Python program to validate user_id in the list of user_ids.
Python program to print a square or cube if the given number is divided by 2 or 3 respectively.
Python program to describe the interview process.