Python

# Python Program to Check Prime Number using While Loop4 min read

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:

1. If the number is less than 2, it is not prime.
2. If the number is 2 or 3, it is prime.
3. 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.

Output:

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:

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:

Example: Here is an example of how you can use a `while` loop to print the first 25 prime numbers in Python

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:

• anna says:

may I get an explanation about how each step was done?

• Spider says:

If user enter “1” this program will print it as prime number

• lol says:

put at the end
if num0:
print(f”{number} is prime”)

• sahil says: