-5

Write source code that classifies all numbers from 2 to 20 as being either a composite number or a prime number.

I am only able to do a code where it's only one number not a range of numbers

num=2
if num > 1:
    for i in range(2, int(num/2)+1):
        if (num % i) == 0:
            print(num,"is a composite number")
        break
    else:
        print(num, "is a prime number")

1 Answers1

0

Try this code snippet below:

prime = [x for x in range(2, 21) if not [y for y in range(2, x) if x % y == 0]]
SuperStar518
  • 2,814
  • 2
  • 20
  • 35
Joost
  • 74
  • 2