Python Armstrong NumberPython Average of N NumbersPython Biggest of Three NumbersPython Common DivisorsPython Composite NumberPython Condense a NumberPython Even NumberPython Factorial of a NumberPython Fibonacci SeriesPython GCDPython LCMPython Leap YearPython Multiplication TablePython Number CombinationPython Odd NumberPython Palindrome NumberPython Perfect NumberPython Prime FactorPython Prime NumberPython Sum of DigitsPython Swapping Two Numbers

Python Array ProgramsPython Converter ProgramsPython Star Patterns ProgramPython Number Patterns ProgramPython Alphabet Patterns ProgramLast Updated:

A positive integer that is divisible only by itself and 1.

**Tips**: It is recommended to use our online Prime Numbers calculator for better understanding.

The following table provides few examples of prime numbers.

Number | Divisor | Result |
---|---|---|

13 | 1, 13 | Prime Number |

15 | 1, 3, 5, 15 | Not a Prime Number |

47 | 1, 47 | Prime Number |

In the following example, we will check whether the given number (7) is a Prime number or not using for loop.

Python Compiler

num = 7
count = 0
for i in range(1, num+1):
if(num % i == 0):
count += 1
if(count == 2):
print("%d is a prime number" % num)
else:
print("%d is a NOT prime number" % num)

In the following example, we will check whether the given number (7) is a Prime number or not using while loop.

Python Compiler

num = 7
i = 1
count = 0
while(num >= i):
if(num % i == 0):
count += 1
i += 1
if(count == 2):
print("%d is a prime number" % num)
else:
print("%d is a NOT prime number" % num)

In the following example, we will find all the Prime numbers between 1 and 20.

Python Compiler

start = 1;
end = 20;
print("Prime numbers between 1 and 20:")
for start in range(start, end+1):
count = 0;
for i in range(1, start+1):
if(start % i == 0):
count += 1
if(count == 2):
print(start, end=" ")

In the following example, we will check whether the given number is a Prime number or Composite number.

Python Compiler

num = int(input("Enter a (int) number: "));
count = 0;
for i in range(1, num+1):
if(num % i == 0):
count += 1;
if(count > 2):
print("%d is a composite number" % num)
else:
print("%d is a prime number" % num)