Here prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Here is source code of the Python Program to Display All the Prime Numbers Between 1 to 100.
Approach 1:
1 2 3 4 5 6 7 8 9 10 11 | print('Prime numbers between 1 and 100 are:') for num in range(2,101): if num > 1: for i in range(2,num): if (num % i) == 0: break else: print(num) |
Approach 2:
1 2 3 4 5 6 7 8 9 10 11 12 | for possiblePrime in range(2, 101): # Assume number is prime until shown it is not. isPrime = True for num in range(2, possiblePrime): if possiblePrime % num == 0: isPrime = False if isPrime: print(possiblePrime) |
Approach 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 | for possiblePrime in range(2, 101): # Assume number is prime until shown it is not. isPrime = True for num in range(2, possiblePrime): if possiblePrime % num == 0: isPrime = False break if isPrime: print(possiblePrime) |
Approach 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 | for possiblePrime in range(2, 101): # Assume number is prime until shown it is not. isPrime = True for num in range(2, int(possiblePrime ** 0.5) + 1): if possiblePrime % num == 0: isPrime = False break if isPrime: print(possiblePrime) |
Output: