Python Program to Check if a Number is a Prime Number














































Python Program to Check if a Number is a Prime Number



Problem Description:

    The program takes in a number and checks if it is a prime number.

Problem Solution:

1. Take in the number to be checked and store it in a variable.
2. Initialize the count variable to 0.
3. Let the for loop range from 2 to half of the number (excluding 1 and the number itself).
4. Then find the number of divisors using the if statement and increment the count variable each time.
5. If the number of divisors is lesser than or equal to 0, the number is prime.
6. Print the final result.
5. Exit.

Python Program/Source Code:
#Python Program to Check if a Number is a Prime Number class prime: def __init__(self,n): self.n=n def isPrime(self): k=0 for i in range(2,self.n//2+1): if(self.n%i==0): k=k+1 if k==0 and self.n!=1: print("Number is prime") else: print("Number isn't prime") def main(): a=int(input("Enter number: ")) num1=prime(a) num1.isPrime() if __name__ == "__main__": main()


Program Explanation:

1. User must enter the number to be checked and store it in a different variable.
2. The count variable is first initialized to 0.
3. The for loop ranges from 2 to the half of the number so 1 and the number itself aren%u2019t counted as divisors.
4. The if statement then checks for the divisors of the number if the remainder is equal to 0.
5. The count variable counts the number of divisors and if the count is lesser or equal to 0, the number is a prime number.
6. If the count is greater than 0, the number isn%u2019t prime.
7. The final result is printed.

Runtime Test Cases:
 
Case 1:
Enter number: 7
Number is prime
 
Case 2:
Enter number: 35
Number isn't prime

More Articles of Anurag Jain:

Name Views Likes
Python Program to Remove the Duplicate Items from a List 770 10
Python Program to Construct a Tree & Perform Insertion, Deletion, Display 790 0
Python Program to Count Number of Leaf Node in a Tree 1485 0
Python Program to Create a Mirror Copy of a Tree and Display using BFS Traversal 920 0
Python GUI application to record a video using Tkinter GUI module and openCV computer vision library 7114 0
Python Program to Find the Number of Nodes in a Binary Tree 830 0
Python Program to Add a Key-Value Pair to the Dictionary 725 0
Python Program to Map Two Lists into a Dictionary 969 0
Python Program for Depth First Binary Tree Search using Recursion 853 0
Python program to get disk name and its vendor from Linux/UNIX like system and return in a dictionary like structure 834 0
Python Program for face Detection using openCV and haar-cascades 1480 0
Python Program to Implement Introsort 1107 0
Python Program to Find the Largest value in a Tree using Inorder Traversal 702 0
Python Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50 1368 23
Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x,x*x) 1056 0
Python Program to Remove the ith Occurrence of the Given Word in a List where Words can Repeat 1446 10
Python Program to Implement Radix Sort 1206 0
Python Program to Print Border of given Tree in Anticlockwise Direction 819 0
Python Program to Print all Numbers in a Range Divisible by a Given Number 987 17
Python Program to Remove All Tuples in a List of Tuples with the USN Outside the Given Range 1132 0
Python Program to Read Height in Centimeters and then Convert the Height to Feet and Inches 879 13
Python Program to Find Nth Node in the Inorder Traversal of a Tree 1055 0
Python Program to Construct a B Tree 5639 0
Python Program to Implement Depth First Search Traversal using Post Order 980 0
Python Program to Sum All the Items in a Dictionary 960 0
Python Program to Check if a Number is a Perfect Number 930 15
Python Program to Build Binary Tree if Inorder or Postorder Traversal as Input 1146 0
Python Program to Form a Dictionary from an Object of a Class 959 0
Python Program to Remove the Given Key from a Dictionary 811 0
Python Program to Count Number of Non Leaf Nodes of a given Tree 788 0
Python Program to Generate Gray Codes using Recursion 960 22
Python Program to Compute Simple Interest Given all the Required Values 890 21
Python Program to Check Whether a Number is Positive or Negative 825 25
Python Program to Read a List of Words and Return the Length of the Longest One 1333 10
Python GUI application to record a video using tkinter GUI module and openCV 62 6
Python Program for Depth First Binary Tree Search without using Recursion 851 0
Python Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character 2473 0
Python program to get disk information, health, short test results using Smartctl one of Smartmoontools for disk analysis using Linux system 884 1
Python Program to Multiply All the Items in a Dictionary 1360 0
Python Program to Construct a Binary Search Tree and perform deletion and inorder traversal 1186 0
Python Program to Concatenate Two Dictionaries Into One 891 0
Python Program to Check if a Number is a Prime Number 765 22
Python Program to Swap the First and Last Value of a List 1105 22
Python Program to Demonstrate Circular Single Linked List 835 0
Python Program to Check if a Given Key Exists in a Dictionary or Not 775 0

Comments