Question:
How to write a java program Given a number n as input, return the value of the nth prime. Note that n is always greater than 0.?
Code:
import java.util.Scanner; public class NthPrime { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter n to compute the nth prime number: "); int nth = sc.nextInt(); int num, count, i; num=1; count=0; while (count < nth){ num=num+1; for (i = 2; i <= num; i++){ if (num % i == 0) { break; } } if ( i == num){ count = count+1; } } System.out.println("Value of nth prime: " + num); } }
Output:
$ java NthPrime Enter n to compute the nth prime number: 10 Value of nth prime: 29