Pythondex
Python Programs

Python Program To Check Composite Number

Last updated June 12, 2023 by Jarvis Silva

In this article I will show you the python program to check composite number, 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.

Python Code For Checking 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 program for checking composite number, now let’s see how this python program actually works:

  1. Prompt the user to enter a number.
  2. Read the input number and convert it to an integer.
  3. Set the variable ‘is_composite’ to False, indicating that the number is initially assumed to be non-composite (prime).
  4. Start a loop that iterates from 2 up to the input number (excluding the number itself).
  5. Check if the input number is divisible evenly by the current iteration value (i).
  6. If the number is divisible, set ‘is_composite’ to True, indicating that it is a composite number.
  7. After the loop ends, check the value of ‘is_composite’.
  8. If ‘is_composite’ is True, print a message stating that the number is composite.
  9. If ‘is_composite’ is False, print a message stating that the number is not composite (prime).

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 or you can use this online python compiler.

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

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

This were all the python programs 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:

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 🙂