In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else statement and for loop along with the different operators of the python programming language.
How to Display Prime Numbers Between Two Intervals?
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# Python Program to Display Prime Numbers Between Two Intervals
first = int(input("Enter the first integer: "))
last = int(input("\nEnter the last integer: "))
print("\nPrime numbers between", first, "and", last, "are:")
for num in range(first, last + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)INPUT:
100 200
OUTPUT:
Enter the first integer: Enter the last integer: Prime numbers between 100 and 200 are: 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
- 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 variablefirstandlastwith the statements/strings("Enter the first integer: ")and("\nEnter the last integer: "), we use theintfunction and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- We declare a
printfunction which displays the statement("\nPrime numbers between", first, "and", last, "are:")after which we open aforloop where therangefor the variablenumis(first, last + 1)followed by a:. - Following that we open another a
forloop where therangefor the variableiis(2,num)followed by a:after which we declare anifstatement with condition(num % i) == 0followed by a:, if the condition is satisfied, thebreakstatement terminates the current loop. - If the condition is not satisfied, it moves to the next step which is the
elsestatement whereprintfunction will print the values ofnumwhich is obtained after the iteration of the loop with the given range. - In the above code, the value of
numwill be within the given range(first, last + 1)and in the secondforloop, the value ofiwill be within the range(2,num). Both the loops will iterate until the condition is satisfied and the value is displayed.
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.