nth prime number java

Solutions on MaxInterview for nth prime number java by the best coders in the world

showing results for - "nth prime number java"
Alonso
05 Aug 2020
1import java.util.Scanner;
2public class NthPrime {
3  public static void main(String[] args) {
4    Scanner sc = new Scanner(System.in);
5    System.out.print("Enter n to compute the nth prime number: ");
6    int nth = sc.nextInt(); 
7    int num, count, i;
8    num=1;
9    count=0;
10 
11    while (count < nth){
12      num=num+1;
13      for (i = 2; i <= num; i++){ //Here we will loop from 2 to num
14        if (num % i == 0) {
15          break;
16        }
17      }
18      if ( i == num){//if it is a prime number
19        count = count+1;
20      }
21    }
22    System.out.println("Value of nth prime: " + num);
23  }
24}
Micaela
14 Nov 2019
140076
2
Alan
26 Jan 2018
178682097