118k views
4 votes
Write a python program that requests a positive integer from the user, determines if it is a composite, a prime or neither prime or composite and prints the message. You can choose to use iterative loops to repeatedly run this script or have the user run the script for every input.

User RussNS
by
5.0k points

1 Answer

3 votes

Answer:

num = int(input("Num: "))

if num > 1:

for i in range(2,num):

if (num % i) == 0:

print(num,"is a composite number")

break

else:

print(num,"is a prime number")

else:

print(num,"is neither prime nor composite")

Step-by-step explanation:

This line prompts user for input

num = int(input("Num: "))

The following if condition check for prime, composite or neither both

if num > 1:

The following iteration checks for prime or composite

for i in range(2,num):

This if condition tests if a valid divisor can be gotten. If yes, then input number is composite

if (num % i) == 0:

print(num,"is a composite number")

break

If otherwise, then input number is prime

else:

print(num,"is a prime number")

All numbers less than 2 are neither composite nor prime

else:

print(num,"is neither prime nor composite")

User Shakurov
by
5.5k points