Python

Python Program to Check Prime Number2 min read

In this python program, we will find whether a number is prime or not. Here we will use the if-else statement once again and the new thing we are going to use is for loop.

Here we take the modulus(%) of the given number(num variable) with every number(i variable) less than the num. And if the remainder shows 0 i.e the variable num is divided by the variable i without leaving any remainder then the variable i is the factor of the variable num and hence it prints the variable num is not a prime number and then breaks from the for loop because there is no need to check for other numbers(variable i). And if none of the numbers between the range is the factor of the variable num then it is a prime number.




Write a program to Check Prime Number in Python

Code:

This Python program takes an input number from the user and determines if it is a prime number or not. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.

The program starts by asking the user to input a number, which is stored as an integer in the variable “num”.

Then, the program checks if the input number is greater than 1. If it is greater than 1, it uses a for loop to check if any number between 2 and the input number (excluding the input number) divides the input number completely. If any number between 2 and the input number divides the input number completely, it prints “num is not a prime number” and the factor that divides the input number completely to the output. If no number between 2 and the input number divides the input number completely, it prints “num is a prime number” to the output.

If the input number is less than or equal to 1, the program prints “num is not a prime number” to the output.

The output will be a string indicating whether the input number is a prime number or not.

Leave a Comment