Check for prime number using for and while loop in Python

In this article, we will learn how to take a number as user input and check if the number is a prime number or not using for loop and while loop in Python.

Before starting, let’s learn a bit about prime numbers.

What is a Prime Number?

A prime number is a positive number that is greater than 1 and cannot be divided by any positive number except 1 and by itself.

Examples of prime numbers are, 2, 3, 5, 7, 11 etc.

A prime number can have only two factors. One will be 1 and the other will be the number itself. Example: 13/1 = 13 and 13/13 = 1, so it is a prime number since it have only two factors 1, 13

How to find a prime number?

To find a prime number, we have to follow these steps:

  1. First, we will check if the number is greater than one (1) or not.
  2. If yes, then we will divide the number between the range 2 upto the number itself. For example, if the number is 13, we will start from 13/2, 13/3, 13/4, and so on until 13/12.

If the number 13 is divisible by any number between 2 to 12, then it’s not a prime number. If not, then it is a prime number.

Note: We start dividing it from 2 because every number can be divided by 1.

Here, to find a prime number we will use:

  1. for… else and if statement, and
  2. while loop

Prime Number Program in Python using for loop

Example to check if a number is a prime number or not in python from user input.

num = int(input('Enter a number: '))
if num > 1:
    for i in range(2, num):
        if(num % i) == 0:
            print('Number is not Prime')
            break
    else:
        print('Number is Prime')

Output:

Enter a number: 13
Number is Prime

Here, we first take the user input using input() method. We wrap it around with an int() to convert the user input to an integer.

Next, we check if the number is greater then 1. If yes, we proceed to the next for else statement.

We then check for the factors between 2 to the number itself using range(2, num).

Note: Since all numbers can be divided by 1, so we used 2 as the start in range() function.

On each iteration, we try to find the factor using (num % i) == 0. If any number satisfies this condition, then the number is not a Prime number and we exit the program using break statement. If not, the number is a prime number.

Prime Number Program in Python using while loop

Here, we will use while loop to check if the user input is a prime number or not.

i=2
num = int(input('Enter a number'))
flag=True

while i < num:
    if(num % i) == 0:
        flag = False
    i = i+1

if flag:
    print('Number is a Prime Number')
else:
    print('Number  is not a Prime Number')

Output:

Enter a number: 7
Number is a Prime Number

Here, we have used a flag variable that is set to True initially.

We have set i variable to 2, because it is the starting range to find the factor in the program.

The variable i is like an index value, which will increase each time the program runs through the while loop.

Next, since any number less than 1 or equal to 1 is not a prime number, so we first check if the num is greater than 1 or not.

If the input number is greater than 1, then the while loop will run and on each iteration +1 will be added to i variable.

And if the program finds any factor between the given range, the flag will be set to False.

And outside the while loop,

  • If the flag is True, the number is a prime number.
  • If the flag is False, the number is NOT a prime number.

Print Prime Number within a given range (between 1 to 100) in python

Here we will print all the prime numbers between the range 1 to 100 in python.

lower_val = 1
higher_val = 100
prime_num = []
for num in range(lower_val, higher_val+1):
    if(num > 1):
        for i in range(2, num):
            if(num % i) == 0:
                break
        else:
            prime_num.append(num)
print(prime_num)

Output:

[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]

Here, we set the lower and upper value within which we will find the prime numbers.

We set the range for the first for loop using range(), we set higher_val+1 to include the 100 number.

In the second for loop, we divide the input number by all the numbers with the range 2 to number, range(2, num).
If we find any factor, we use the break statement, to exit the loop. If not, the number gets appended to the empty list prime_num.

At last, we print out the numbers in prime_num python list.

Conclusion: Here, we have learned, how to write python programs to find all prime numbers in within a given range, and how to use the for and while loop to check for prime numbers in python.


Related Topics:

Python – Check if list is empty or not

Check if user input is empty in python

Python – Check if a string is Empty or Not