Given the upper limit range, the task is to print all the prime numbers in the given range in Python.

**Examples:**

**Example1:**

**Input:**

given number = 95

**Output:**

The prime numbers from 2 to upper limit [ 95 ] : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89

**Example2:**

**Input:**

given number = 52

**Output:**

Enter some random upper limit range = 52 The prime numbers from 2 to upper limit [ 52 ] : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

- Python Program to Print all Disarium Numbers within Given range
- Python Program to Generate Strong Numbers in an Interval
- Python Program to Check if a Number is Peterson Number

## Python Program to Print all the Prime Numbers within a Given Range

There are several ways to print all the prime numbers from 2 to the upper limit range some of them are:

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

### Method #1:Using for loop(Static Input)

**Approach:**

- Give the number as static input and store it in a variable.
- Set the first for loop to have a range of 2 to the upper limit range.
- Take a temporary variable which is the count variable and initialize it to zero.
- Allow the second for loop to have a range of 2 to half the number (excluding 1 and the number itself).
- Then, using the if statement, determine the number of divisors and increment the count variable each time.
- The number is prime if the number of divisors is lower than or equal to zero.
- Prime numbers till the given upper limit range are printed.
- The Exit of the Program.

**Below is the implementation:**

# Give the number as static input and store it in a variable. givenNumbr = 95 print('The prime numbers from 2 to upper limit [', givenNumbr, '] :') # Set the first for loop to have a range of 2 to the upper limit range. for valu in range(2, givenNumbr+1): # Take a temporary variable which is the count variable and initialize it to zero. cntvar = 0 # Allow the second for loop to have a range of 2 to # half the number (excluding 1 and the number itself). for divi in range(2, valu//2+1): # Then, using the if statement, determine the number of divisors # and increment the count variable each time. if(valu % divi == 0): cntvar = cntvar+1 # The number is prime if the number of divisors is lower than or equal to zero. if(cntvar <= 0): print(valu, end=' ')

**Output:**

The prime numbers from 2 to upper limit [ 95 ] : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89

All the prime numbers till the upper limit range are printed.

### Method #2:Using for loop(User Input)

**Approach:**

- Give the number as user input with the help of the int(input()) function and store it in a variable.
- Set the first for loop to have a range of 2 to the upper limit range.
- Take a temporary variable which is the count variable and initialize it to zero.
- Allow the second for loop to have a range of 2 to half the number (excluding 1 and the number itself).
- Then, using the if statement, determine the number of divisors and increment the count variable each time.
- The number is prime if the number of divisors is lower than or equal to zero.
- Prime numbers till the given upper limit range are printed.
- The Exit of the Program.

**Below is the implementation:**

# Give the number as user input with the help # of the int(input()) function and store it in a variable. givenNumbr = int(input('Enter some random upper limit range = ')) print('The prime numbers from 2 to upper limit [', givenNumbr, '] :') # Set the first for loop to have a range of 2 to the upper limit range. for valu in range(2, givenNumbr+1): # Take a temporary variable which is the count variable and initialize it to zero. cntvar = 0 # Allow the second for loop to have a range of 2 to # half the number (excluding 1 and the number itself). for divi in range(2, valu//2+1): # Then, using the if statement, determine the number of divisors # and increment the count variable each time. if(valu % divi == 0): cntvar = cntvar+1 # The number is prime if the number of divisors is lower than or equal to zero. if(cntvar <= 0): print(valu, end=' ')

**Output:**

Enter some random upper limit range = 52 The prime numbers from 2 to upper limit [ 52 ] : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

**Explanation:**

- The user must enter the range’s upper limit and save it in a separate variable.
- The first for loop runs until the user enters an upper limit.
- The count variable is initially set to 0.
- Because the for loop runs from 2 to half of the number, 1 and the number itself is not considered divisors.
- If the remainder is equal to zero, the if statement looks for the number’s divisors.
- The count variable counts the number of divisors, and if the count is less than or equal to zero, the integer is prime.
- If the count exceeds zero, the integer is not prime.
- All the prime numbers till the upper limit range are printed.

**Related Programs**:

- Python Program to Print all Numbers in a Range Divisible by a Given Number
- Python Program to Print Odd Numbers Within a Given Range
- Python Program to Read a Text File and Print all the Numbers Present in the Text File
- Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
- Python Program to Calculate the Average of Numbers in a Given List
- Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given Range of Numbers