# Write a Program to Find the nth Prime Number in C

Last updated:11th Feb 2022

In this program, you will take input from the user and you will find the nth prime number in C. prime numbers are 2,3,5,7,11.....n.

input:3 output:5 input:5 output:11 input:13 output:41

For example, if the user input is 3 then the output will be 5 because a third prime number is 5.

## program to find nth prime number in c

```
#include<stdio.h>
int main()
{
int num,PrimeCount=0,i,flag,prime=1;
printf("\n enter the number:");
scanf("%d",&num);
while(num!=PrimeCount)
{
flag=0;
prime++;
for(i=2;i<=(prime/2);i++)
{
if(prime%i==0)
flag=1;
}
if(flag==0)
{
PrimeCount++;
}
}
printf("%d prime number is: %d",num,prime);
return 0;
}
```

## output

enter the number:3 3 prime number is: 5

### explanation of Nth prime number

- you will take a number from the user and store it in a variable
`num`

. For example, the user wants to find the 3rd prime number i.e num=3. - Iterate
`while`

loop until you find the nth prime. - Inside
`while`

loop, you will find a prime number between 1 to nth.`prime++; for(i=2;i<=(prime/2);i++) { if(prime%i==0){ flag=1; } }`

- PrimeCount value gets incremented when you find any prime number.
`if(flag==0) { PrimeCount++; }`

- when you get the 1st prime number, your PrimeCount value will be 1 and when you get the 2nd prime number, your PrimeCount value will be 2. when your PrimeCount value becomes 3, while loop gets end and it prints the nth prime number value.

### Related program

C program to print all prime numbers between 1 to n or lower limit to upper limit.

C program to check whether the number is prime or not using for loop, while loop, do-while loop.