In the previous article, we have discussed Python Program to Delete Random Item from a List

**Prime Number :**

A prime number is one that can only be divided by one and itself.

Given a number ‘n’, and the task to find the nth prime number.

**Examples:**

**Example1:**

**Input:**

Given number = 8

**Output:**

The above given nth Prime Number is = 19

**Example2:**

**Input:**

Given number = 3

**Output:**

The above given nth Prime Number is = 5

## Program to Find nth Prime Number

Below are the ways to find the nth prime number.

### Method #1: Using While, For Loop (Static Input)

**Approach:**

- Give the number say ‘n’ as static input and store it in a variable.
- Take a list say ‘prime_numbers’ and initialize it with 2, 3 values.
- Take a variable and initialize it with 3 say ‘x’.
- Check if the given number is between greater than 0 and less than 3 using the if conditional statement.
- If the statement is true, then print the value of the list “prime_numbers[n-1] “.
- Check if the given number is greater than 2 using the elif conditional statement.
- Iterate the loop infinite times using the while loop i.e while(True).
- Inside the loop increment the value of the above given ‘x’ variable by ‘1’.
- Take a variable to say ‘y’ and initialize with ‘True’.
- Loop from 2 to int((x/2)+1) using the for loop and int function().
- Check if the value of variable ‘x’ modulus iterator value is equal to zero or not using the if conditional statement.
- If the statement is true, assign “False” to the variable y, break the statement and come out of the loop.
- Check if variable y is equal to True using the if conditional statement.
- If the statement is true, then append the value of variable ‘x’ to the above list ‘prime_numbers’.
- Check if the length of the above listÂ ‘prime_numbers’ is equal to the given number.
- If the statement is true, then give a break statement and come out of the while loop.
- Print the value of the list “prime_numbers[n-1]” to get the nth prime number.
- Else print “Invalid number. Please enter another number “.
- The Exit of the Program.

**Below is the implementation:**

# Give the number say 'n' as static input and store it in a variable. num = 5 # Take a list say 'prime_numbers' and initialize it with 2, 3 values. prim_numbrs = [2, 3] # Take a variable and initialize it with 3 say 'x'. x = 3 # Check if the given number is between greater than 0 and less than 3 using the if # conditional statement. if(0 < num < 3): # If the statement is true, then print the value of the list "prime_numbers[n-1] ". print('The above given nth Prime Number is =', prim_numbrs[num-1]) # Check if the given number is greater than 2 using the elif conditional statement. elif(num > 2): # Iterate the loop infinite times using the while loop i.e while(True). while (True): # Inside the loop increment the value of the above given 'x' variable by '1'. x += 1 # Take a variable say 'y' and initialize with 'True'. y = True # Loop from 2 to int((x/2)+1) using the for loop and int function(). for itr in range(2, int(x/2)+1): # Check if the value of variable 'x' modulus iterator value is equal to zero or not # using the if conditional statement. if(x % itr == 0): # If the statement is true, assign "False" to the variable y, break the statement and # come out of the loop. y = False break # Check if variable y is equal to True using the if conditional statement. if(y == True): # If the statement is true, then append the value of variable 'x' to the # above list 'prime_numbers'. prim_numbrs.append(x) # Check if the length of the above list 'prime_numbers' is equal to the given number. if(len(prim_numbrs) == num): # If the statement is true, then give a break statement and come out of the while loop. break # Print the value of the list "prime_numbers[n-1]" to get the nth prime number. print('The above given nth Prime Number is = ', prim_numbrs[num-1]) # Else print "Invalid number. Please enter another number ". else: print("Invalid number. Please enter another number ")

**Output:**

The above given nth Prime Number is = 11

### Method #2: Using While, For Loop (User Input)

**Approach:**

- Give the number say ‘n’ as user input using int(input()) and store it in a variable.
- Take a list say ‘prime_numbers’ and initialize it with 2, 3 values.
- Take a variable and initialize it with 3 say ‘x’.
- Check if the given number is between greater than 0 and less than 3 using the if conditional statement.
- If the statement is true, then print the value of the list “prime_numbers[n-1] “.
- Check if the given number is greater than 2 using the elif conditional statement.
- Iterate the loop infinite times using the while loop i.e while(True).
- Inside the loop increment the value of the above given ‘x’ variable by ‘1’.
- Take a variable to say ‘y’ and initialize with ‘True’.
- Loop from 2 to int((x/2)+1) using the for loop and int function().
- Check if the value of variable ‘x’ modulus iterator value is equal to zero or not using the if conditional statement.
- If the statement is true, assign “False” to the variable y, break the statement and come out of the loop.
- Check if variable y is equal to True using the if conditional statement.
- If the statement is true, then append the value of variable ‘x’ to the above list ‘prime_numbers’.
- Check if the length of the above listÂ ‘prime_numbers’ is equal to the given number.
- If the statement is true, then give a break statement and come out of the while loop.
- Print the value of the list “prime_numbers[n-1]” to get the nth prime number.
- Else print “Invalid number. Please enter another number “.
- The Exit of the Program.

**Below is the implementation:**

# Give the number say 'n' as user input using int(input()) and store it in a variable. num = int(input("Enter some random number = ")) # Take a list say 'prime_numbers' and initialize it with 2, 3 values. prim_numbrs = [2, 3] # Take a variable and initialize it with 3 say 'x'. x = 3 # Check if the given number is between greater than 0 and less than 3 using the if # conditional statement. if(0 < num < 3): # If the statement is true, then print the value of the list "prime_numbers[n-1] ". print('The above given nth Prime Number is =', prim_numbrs[num-1]) # Check if the given number is greater than 2 using the elif conditional statement. elif(num > 2): # Iterate the loop infinite times using the while loop i.e while(True). while (True): # Inside the loop increment the value of the above given 'x' variable by '1'. x += 1 # Take a variable say 'y' and initialize with 'True'. y = True # Loop from 2 to int((x/2)+1) using the for loop and int function(). for itr in range(2, int(x/2)+1): # Check if the value of variable 'x' modulus iterator value is equal to zero or not # using the if conditional statement. if(x % itr == 0): # If the statement is true, assign "False" to the variable y, break the statement and # come out of the loop. y = False break # Check if variable y is equal to True using the if conditional statement. if(y == True): # If the statement is true, then append the value of variable 'x' to the # above list 'prime_numbers'. prim_numbrs.append(x) # Check if the length of the above listÂ 'prime_numbers' is equal to the given number. if(len(prim_numbrs) == num): # If the statement is true, then give a break statement and come out of the while loop. break # Print the value of the list "prime_numbers[n-1]" to get the nth prime number. print('The above given nth Prime Number is = ', prim_numbrs[num-1]) # Else print "Invalid number. Please enter another number ". else: print("Invalid number. Please enter another number ")

**Output:**

Enter some random number = 1 The above given nth Prime Number is = 2

Explore more instances related to python concepts fromÂ Python Programming ExamplesÂ Guide and get promoted from beginner to professional programmer level in Python Programming Language.

- Python Program to Find Even Digits Sum and Odd Digits Sum Divisible by 4 and 3 Respectively
- Python Program to Check Whether Sum of digits at Odd places of a Number is Divisible by K
- Python Program to Check Whether Product of Digits at Even places of a Number is Divisible by K
- Python Program to Check if Product of Digits of a Number at Even and Odd places is Equal