In this python 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.
RUN CODE SNIPPET# 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")
INPUT:
59
OUTPUT:
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
input
function and are stored in the variablenum
with the statements/strings("Enter a number: ")
, we use theint
function 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
if
statement followed by a:
with conditionnum > 1
where if the condition is satisfied, it moves to the next statement, that is thefor
loop, if the condition is not satisfied it will move to the last step which is theelse
statement. - In the last
else
statement, theprint
function will display the statement("\n", num,"is not a prime number")
where the variablenum
holds the input integer value. - Following the first
if
statement, we open afor
loop where therange
for the variablei
is(2,num)
followed by a:
. - In the
for
loop, we declare anif
statement with condition(num % i) == 0
followed by a:
, if the condition is satisfied, theprint
function will display the statement("\n", num,"is not a prime number")
andbreak
statement terminates the current loop. - In the above line of code, the value of
num
will 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
else
statement, where theprint
function will display the statement("\n", num,"is a prime number")
.
NOTE:
- 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.