-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathNthPrimeNumber.java
33 lines (33 loc) · 919 Bytes
/
NthPrimeNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;
public class NthPrimeNumberExample
{
public static void main(String[] args)
{
//constructor of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n to compute the nth prime number: ");
//reading an integer from the user
int n = sc.nextInt();
int num=1, count=0, i;
while (count < n)
{
num=num+1;
for (i = 2; i <= num; i++)
{
//determines the modulo and compare it with 0
if (num % i == 0)
{
//breaks the loop if the above condition returns true
break;
}
}
if (i == num)
{
//increments the count variable by 1 if the number is prime
count = count+1;
}
}
//prints the nth prime number
System.out.println("The " +n +"th prime number is: " + num);
}
}