Python Program to Check Whether a Number is Prime or Not
- More than 40 Lakhs Students use Grammarly for their spell and grammar checks. You can register and try for free.
In this tutorial, you will learn how to Check Whether a Number is Prime or Not using the if and else statement and for loop along with the different operators of the python programming language.
How to Check Whether a Number is Prime or Not?
Let’s take a look at the source code , here the values are given as input by the user in the code, the if, else statement and for loop along with the assignment operators carry out the function.
# Program to check if a number is prime or not using loop num = int(input("Enter a number: ")) if num > 1: for i in range(2,num): if (num % i) == 0: print("\n", num,"is not a prime number") break else: print("\n", num,"is a prime number") else: print("\n", num,"is not a prime number")
Enter a number: 59 is a prime number
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in the variable
numwith the statements/strings
("Enter a number: "), we use the
intfunction and declare the input value as an integer value.
- In the STDIN section of the code editor the input values are entered.
- Next we declare the
ifstatement followed by a
num > 1where if the condition is satisfied, it moves to the next statement, that is the
forloop, if the condition is not satisfied it will move to the last step which is the
- In the last
("\n", num,"is not a prime number")where the variable
numholds the input integer value.
- Following the first
ifstatement, we open a
forloop where the
rangefor the variable
(2,num)followed by a
- In the
forloop, we declare an
ifstatement with condition
(num % i) == 0followed by a
:, if the condition is satisfied, the
("\n", num,"is not a prime number")and
breakstatement terminates the current loop.
- In the above line of code, the value of
numwill be within the given range
(num % i) == 0, and the loop will iterate until the condition is satisfied.
- If not it moves the next step which is the
elsestatement, where the
("\n", num,"is a prime number").
- A For loop runs a block of code until the loop has iterated over every item in a condition, that is it helps to reduce any repetition in the code because it executes the same operation multiple times.
- The Range() function is used to generate a sequence of numbers, where the range() function is commonly used in for looping functions.
- The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
- The Break statement in Python terminates the current loop and resumes execution at the next statement.
- The print statement/string to be displayed in enclosed in double quotes.
- The input() function allows a user to insert a value into a program, it returns a integer value.
- The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.