Python Program to Display Prime Numbers Between Two Intervals

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
  1. 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 variable first and last with the statements/strings  ("Enter the first integer: ") and ("\nEnter the last integer: "),  we use the int function and declare the  input value as an integer value.
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare a print function which displays the statement ("\nPrime numbers between", first, "and", last, "are:") after which we open a for loop where the range for the variable num is (first, last + 1) followed by a :.
  4. Following that we open another a for loop where the range for the variable i is (2,num) followed by a : after which we declare an if statement with condition (num % i) == 0 followed by a :, if the condition is satisfied, the break statement terminates the current loop.
  5. If the condition is not satisfied, it moves to the next step which is the else statement where print function will print the values of num which is obtained after the iteration of the loop with the given range.
  6. In the above code, the value of num  will be within the given range (first, last + 1) and in the second for loop, the value of i will be within the range (2,num). Both the loops will iterate until the condition is satisfied and the value is displayed.

NOTE:

  • 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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...
Merge Sort is a popular sorting algorithm that follows the divide-and-conquer paradigm to efficiently sort an array or list of...