Program to Check Prime Number in Python

Program to Check Prime Number in Python. In this program. We will learn about the prime number and how to detect the it using if else condition and For loop. It is basic and conceptual program for beginners, class 11,12 and B Tech computer science students. Download source code.

Python Program to Check Prime Number

We will write a program here in which we will check that a given number is a prime number or not.

What is a Prime Number ?

If the natural number is greater than 1 and having no positive divisors other than 1 and the number itself etc is called as prime number.
For example: 3, 7, 11 etc are prime numbers.

Logic of Program

We have used nested if else condition to check if the number is a prime number or not.

First, we have checked if the given number is greater than 1 or not. If it is not greater than 1, then the number will directly come to the else part and print ‘not a prime number.’
If number is bigger than 1, the number will enter into for loop where we perform Iteration from 2 to number/2. Then, we used nested if else condition inside the for loop. If the number is completely divisible by ‘i’ then, it is not a prime number; else, the number is a prime number.

Source Code of Program to Check Prime Number in Python

  
def PrimeChecker(a):  
      
    if a > 1:  
          
        for j in range(2, int(a/2) + 1):  
              
            if (a % j) == 0:  
                print(a, "is not a prime number")  
                break  
          
        else:  
            print(a, "is a prime number")  
      
    else:  
        print(a, "is not a prime number")  
  
a = int(input("Enter an input number:"))   
PrimeChecker(a)  

Download Source code : For Download Click on the below Button

Step 1 : A default function for Prime checking conditions  
def PrimeChecker(a):  
Step 2 : Checking that given number is more than 1  
  if a > 1:  
Step 3 : Iterating over the given number with for loop  
      for j in range(2, int(a/2) + 1):  
Step 4 :   If the given number is divisible or not  
          if (a % j) == 0:  
                print(a, “is not a prime number”)  
                break  
Step 5 : Else it is a prime number  
       else:  
            print(a, “is a prime number”)  
Step 6 : If the given number is 1  
    else:  
        print(a, “is not a prime number”)  
Step 7: Taking an input number from the user  
a = int(input(“Enter an input number:”))  
Step 8 : Printing result  
PrimeChecker(a)  

Output:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Enter an input number:56
56 is not a prime number
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Enter an input number:13
13 is a prime number
>>> 

Output Explanation :

56 is captured by the program
Enter an input number:56
It runs the program and print the result
56 is not a prime number
Similarly,
Enter an input number:13
13 is a prime number

Conclusion :

Program is running fine and giving correct output according to the input.

Thank you for reading and learning from Program to Check Prime Number in Python post. . In this program theoretical mathematics knowledge of prime number, calculation of prime number, if else condition and For loop. It is basic and conceptual program for beginners, class 11,12 and B Tech computer science students. Below are the links for more basic programs and practical file from class 11 and 12.

Python Programs Archives – CS Study

Computer Science Practical file for class 12 Term-2 – CS Study

Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study

Leave a Comment

Your email address will not be published.