In this post, we will write a program in Python to check whether the input number is prime or not using while loop.
What are Prime Numbers
Prime numbers are natural numbers (positive integers) that are greater than 1 and have no positive integer divisors other than 1 and themselves. For example, the first few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
Prime numbers are often used in various cryptographic protocols because of their unique factorization properties. For example, it is easy to check if a number is prime, but it is difficult to find the prime factors of a large composite number. This makes it difficult for an attacker to break certain types of encrypted messages, because the attacker would need to factorize a large composite number in order to decrypt the message.
You can use the following test to determine if a number is prime:
- If the number is less than 2, it is not prime.
- If the number is 2 or 3, it is prime.
- Otherwise, check if the number is divisible by any integer between 2 and the square root of the number. If it is not divisible by any of these integers, it is prime. Otherwise, it is composite.
How to write Prime number Program in Python?
- Prime number is a number that is greater than 1 and divided by 1 or itself.
- In other words, prime numbers can’t be divided by other numbers than itself or 1.
- For example- 2, 3, 5, 7, 11, 13, 17, 19, 23…. are the prime numbers.
- Let’s see the prime number program in Python.
In this Python program, we will take an input from the user and check whether the number is prime or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
num = int(input("Enter a number ( greater than 1)")) f = 0 i = 2 while i <= num / 2: if num % i == 0: f=1 break i=i+1 if f==0: print("The entered number is a PRIME number") else: print("The entered number is not a PRIME number") |
Output:
1 2 3 4 |
Enter a number: 19 The entered number is a PRIME number |
Example:
Here is a simple example of how you can use a while
loop to check if a number is prime or not in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def is_prime(n): if n <= 1: return False i = 2 while i*i <= n: if n % i == 0: return False i += 1 return True # Test the function for n in range(2, 10): print(f"{n}: {is_prime(n)}") |
if n
is a prime number, and False
otherwise. The function first checks if n
is less than or equal to 1. If this is the case, it returns False
immediately, since 1 is not considered a prime number. Otherwise, the function initializes a variable i
to 2 and enters a loop that continues as long as i*i
is less than or equal to n
. Inside the loop, the function checks if n
is divisible by i
. If this is the case, it returns False
immediately, since n
is not a prime number. If n
is not divisible by i
, the function increments i
by 1 and continues to the next iteration of the loop. If the loop completes and no divisors of n
have been found, the function returns True
, indicating that n
is a prime number.
You may also like: Python Tutorial with Exercises(100+ Examples)
The code then tests the function by calling it in a loop for the values 2 through 10 and printing the output. The expected output is:
1 2 3 4 5 6 7 8 9 10 |
2: True 3: True 4: False 5: True 6: False 7: True 8: False 9: False |
Example: Here is an example of how you can use a while
loop to print the first 25 prime numbers in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
def is_prime(n): if n <= 1: return False i = 2 while i*i <= n: if n % i == 0: return False i += 1 return True # Print the first 25 prime numbers n = 2 count = 0 while count < 25: if is_prime(n): print(n) count += 1 n += 1 |
This code defines the is_prime
function as before, and then uses it to print the first 25 prime numbers. The code initializes a variable n
to 2 and a variable count
to 0. It then enters a loop that continues as long as count
is less than 25. Inside the loop, the code calls the is_prime
function on n
and, if the result is True
, it prints n
and increments count
by 1. After the is_prime
function is called, the code increments n
by 1 and continues to the next iteration of the loop.
This code will print the first 25 prime numbers, which are:
This will output the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 |
may I get an explanation about how each step was done?
If user enter “1” this program will print it as prime number
put at the end
if num0:
print(f”{number} is prime”)
add close bracket
num = int(input(“Enter a number ( greater than 1)”))