Write a python program to input a number and check if the number is prime or composite number using nested if-else loop
This python program for Prime and composite number allows the user to enter any integer value and checks whether the given number is a Prime number or composite number using if-else Loop.
What is Prime number?
Any natural number that is divisible by 1 and itself called Prime Number in Python. Prime number is not divisible by any other numbers except one and itself.
Prime Numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109 etc
What is composite number?
Any positive integer that can be formed by multiplying two smaller positive integers is called composite number. In other word, Composite number is a positive integer that has at least one divisor other than 1 and itself.
We can say that composite numbers are exactly the numbers that are not prime and not a unit.
Composite numbers are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 102, 104, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 129, 130, 132, 133, 134, 135, 136, 138, 140, 141, 142, 143, 144, 145, 146, 147, 148, 150 etc.
For example:
For example, the integer 4 is a composite number because it is the product of the two smaller integers 2 X 2 Likewise, the integer 2 is not composite numbers because it can only be divided by one and itself.
Program Logic:
- Take a any number from user using input method
- Use if-elif statement to check number is zero or 1
- If number is zero or one then given number is neither prime number nor composite number
- If user entered number is negative number then program ask user to enter only positive number.
- If number is greater than one then only number can be either prime number or composite number.
- Use for loop to iterate through number
- Use if statement within for loop to check number is prime number or composite number
- If given condition is true then number is composite number otherwise prime number.
Below is implementation / source code
#Input a number and check if the number is prime or composite number
n= int(input("Enter any number:"))
if(n ==0 or n == 1):
printf(n,"Number is neither prime nor composite")
elif n>1 :
for i in range(2,n):
if(n%i == 0):
print(n,"is not prime but composite number")
break
else:
print(n,"number is prime but not composite number")
else :
print("Please enter positive number only ")
Program Description:
Within the for loop, there is for statement to check whether the Number divisible by i is exactly equal to 0 or not. If the condition is True, then entered number is not prime number; it will be composite number and then break statement is executed. If condition is false,then given number is prime number but not composite number.
Below is output
>>> %Run 'compositeor prime.py' Enter any number:3 3 number is prime but not composite number >>> %Run 'compositeor prime.py' Enter any number:2 2 number is prime but not composite number >>> %Run 'compositeor prime.py' Enter any number:8 8 is not prime but composite number
Below is Snapshot of complete code with output

Python Examples:
- Python program to input welcome message and display it
- Python program to input two numbers and display the larger/smaller number.
- Python program to input three numbers and display the larger/smaller number.