Check Composite Number
Check whether a number is composite (non-prime, greater than 1).
BeginnerTopic: Loop Programs
Python Check Composite Number Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to check if a number is composite
n = int(input("Enter an integer: "))
if n <= 1:
print(n, "is neither prime nor composite")
else:
is_composite = False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
is_composite = True
break
if is_composite:
print(n, "is a composite number")
else:
print(n, "is not a composite number (it is prime)")Output
Enter an integer: 9 9 is a composite number
Understanding Check Composite Number
We look for any divisor between 2 and √n; if found, the number is composite.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.