This is a Python Program to check if a number is a prime number.
The program takes in a number and checks if it is a prime number.
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.
Here is source code of the Python Program to check if a number is a prime number. The program output is also shown below.
a=int(input("Enter number: ")) k=0 for i in range(2,a//2+1): if(a%i==0): k=k+1 if(k<=0): print("Number is prime") else: print("Number isn't prime")
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’t 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’t prime.
7. The final result is printed.
Case 1: Enter number: 7 Number is prime Case 2: Enter number: 35 Number isn't prime
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.