Program to check if the user input is a composite number or not in Python

(247 Views)


What is a Composite Number?

A composite number is a positive integer number that is not prime number. In other words, it is a positive integer number which has at least one divisor other than 1 and itself. The composite numbers up to 20 are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20.

For example:
Input: 10
Output: yes, its Composite (Because 10 has divisors other than 1 and itself, for ex, 2 or 5).

Input: 5
Output: no, its not Composite (Because 5 is a prime number, as it has no other divisors other than 1 and itself). Write a program to check if the user input is a composite number or not using Python

Problem Statement:

Write a program to check if the user input is a composite number or not in Python


Code:

def composite(n): if n <=1: return("Number less than or equal to 1!") else: for x in range(2,n): if n%x==0: return("composite") return("Non composite, it's a prime number") try: n=int(input()) print("Input:",n) print(composite(n)) except: print("Error!!")

Solution Worked 2 UpvotesUpvote

        

Solution Didn't Worked 1 DownvotesDownvote



Comments



Search


Search Tags

    Composite number python

    Composite number or not program in python

    how to check if a number is composite