Python Program To Check Composite Number

Want to know how to create a python program to check if a number is composite or not, then you are at the right place today. In this article I will show you the python program to check composite number so read till the end.
A composite number is a positive integer which has more than two factors. For example, 10 has factors 1, 2, 5, 10, hence it is a composite number.
So today I will show you how to create this program to check composite number in python. You just need to have basic knowledge of python.
Python Program To Check Composite Number
number = int(input("Enter a number : "))
is_composite = False
for i in range(2, number):
if number % i == 0:
is_composite = True
if is_composite == True:
print(number, "is a composite number")
else:
print(number, "is not a composite number but a prime number")
Above is the python program to check composite number. Now to run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer.
To run this python program, follow the below steps:
- Create a new folder for this python project.
- Open it in a code editor of your choice.
- Create a python file with an ending .py extension.
- Copy the above code and paste it in your file.
- Open a terminal or CMD at folder location
- And type this command python filename.py (Name of your file).
After running this program you will be asked to enter a number after that it will tell if the number is composite or not like below output.
Output
Enter a number : 6
6 is a composite number
Don’t want to create all the files and folders then you can run this program now using this online python compiler it is fast and easy.
Code Explaination
To check if a number is composite or not you need to perform a divisibility test on the less positive integers then the number for example our number is 6 so the less positive integers would be 5,4,3,2.
So we need to see if we get number six in those less positive integers so let’s see in 5 we do not get 6, in 4 we do not get 6, in 3 we get 6 (3×2) and in 2 we get 6 (2×3) so we have 4 factors here 1,2,3 and 6.
We know that if a number has more then 2 factors it is said to be a composite number. Note that a number will always have 2 factors which are 1 and itself.
So in our program we run a for loop from number 2 to the user entered number and we divide the number by user number and if it gives the remainder 0 then we set the number to be composite.
I hope you understood how this program works, Below are some more programs on composite numbers in python.
Program To Display All Composite Numbers From 1 to 100 In Python
for number in range(1,101):
for i in range(2,number):
if number % i == 0:
print(number)
break
Output
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
Python Program To Print Composite Numbers From 1 to N
n = int(input("Enter amount of composite numbers you want to find: "))
for number in range(1, n + 1):
for i in range(2,number):
if number % i == 0:
print(number)
break
Output
Enter amount of composite numbers you want to find: 20
4
6
8
9
10
12
14
15
16
18
20
Summary
This was the python program to check whether a number is composite or not. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might be interested in this program.
Here are some more python drawing tutorials for you:
- Python Program To Convert MegaBytes To Bytes.
- Python Program To Check Even Or Odd Numbers.
- Python Program To Convert Miles To Feet.
- Python Program To Convert Rupee To Paise.
I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our Telegram channel for future updates.
Thanks for reading, have a nice day 🙂