Prime and Composite numbers with python

Python program to get prime and composite numbers.

startNumber = 1
endNumber = 20
prime = []
composite = []
for i in range(startNumber,endNumber):
        for p in range(2, i):
            if (i % p)==0:
                composite.append(i)
                break
        else:
            prime.append(i)
print("prime: ",prime)
print("composite: ",composite)

#Output
prime: [1, 2, 3, 5, 7, 11, 13, 17, 19]
composite: [4, 6, 8, 9, 10, 12, 14, 15, 16, 18]

Aman Kharwal
Aman Kharwal

Data Strategist at Statso. My aim is to decode data science for the real world in the most simple words.

Articles: 1607

Leave a Reply

Discover more from thecleverprogrammer

Subscribe now to keep reading and get access to the full archive.

Continue reading