To generate nth Prime number
The nth prime number will be the nth position in the list of natural numbers after removing all the composites.
For example:-
1st prime number is 2.
2nd prime number is 3.
4th prime number is 7.
#Function to generate nth Prime Number nthPrime(n): initialize counter variable to 0 and num variable to 1 while counter is less than n: increment num by 1 for i=2 to num: #Determines the modulo and compare it to zero. if(num%i==0): #breaks the loop if the above condition returns true break if(num==i) :#if denominator is same as numerator increments the counter variable by 1 if the number is prime EndFor EndWhile print("The nth prime numer is "+ num) #Print the prime number
With the while loop, we can execute a set of statements as long as a condition is true
SYNTAX:-
While(condition):
//code block to be executed
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
SYNTAX:-
for counter in sequence:
//code block to be executed
With the break statement, we can come out of the loop even if, while condition is true.
Steps to follow are as follows:
Hence, we have got an idea about prime numbers and can find the value of nth Prime Number using the above code.
Mr. Harsh Bamotra, B.Sc(H) Computer Science, II year.
Mentors:
Prof. Sharanjit Kaur,
Ms. Gunjan Rani