Prime Number Program in Python using while loop

Prime Number Program in Python using while loop: we have a number given by the user and we have to print whether that number is prime or not.

In this program, we have a number given by the user. We have to print whether that number is prime or not.

    Prime Number Program in Python using while loop

    In this program, we will find whether the given number is prime or not. We will use the while loop to check that the given number is divisible by any number between 2 and the square root of the given number. It will reduce our time complexity from O(n) to O(sqrt(n)).

    num=int(input("Enter a number: "))
    check=True
    i=2;
    while i*i<=num:
        if(num%i==0):
            check=False
            break
        i+=1
    
    if(check and num!=1):
        print(str(num)+" is a Prime Number")
    else:
        print(str(num)+" is not a Prime Number")
    
    Output:

    Enter a number: 16
    16 is not a Prime Number

    Prime Number Program in Python using for loop

    In this program, we will make a python program to check prime numbers. It will print whether the given number is prime or not.

    # check given number is prime or not
    num=int(input("Enter a number: "))
    check=True
    
    for i in range(2,num):
        if(num%i==0):
            check=False
            break
    
    if(check and num!=1):
        print(str(num)+" is a Prime Number")
    else:
        print(str(num)+" is not a Prime Number")
    
    Output:

    Enter a number: 997
    997 is a Prime Number
    Enter a number: 15
    15 is not a Prime Number

    Prime number checker in python using function

    def prime_checker(number):
        f=1
        for i in range (2,number):
            if(number%i==0):
                f=0
                break
        
        if f:
            print("It's a prime number.")
        else:
            print("It's not a prime number.")
    
    n = int(input("Check this number: "))
    prime_checker(number=n)
    

    Approach for Python program to check prime number:

    We will write a python program to find whether a given number is prime or not.
    Prime Number is a number that is divisible only by one and itself.
    Let's take a number here, so let's say 11:



    We will try to divide this number by two because all the numbers are divisible by one, so we will start with 2. We will check if 11 can be divided by two; if yes, then it's not a prime number; if it's not divisible by "2," then we'll go for three, then we'll go for four, and so on till 10.

    We start from 2, and we'll end with the given number minus 1. For example: if we take a number that is 17, then we will end at 16.

    Every time you have to check if you find the number which is divisible by any other number between 2 & one less than the given number, then the given number is not a prime number. We can print that the given number is a prime number when we find that the given number is not divisible by the last number (which is number-1).  

    About this post:

    This post is all about the prime number program in python. If you have doubts related to this program, let me know in the comment section.

    Post a Comment

    Please do not enter any spam link in the comment box.