Java Program - Nth Prime Number

Mathematics for Programming Numbers (Article) Numbers (Program)

18

Given Input:


Expected Output:


Program:

public class NthPrimeNumber {
    public static void main(String[] args) {
        int n = 10;
        int count = 0, num = 1, i;

        while (count < n) {
            num++;
            for (i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    break;
                }
            }
            if (i > num / 2) {
                count++;
            }
        }
        System.out.println("The " + n + "th prime number is: " + num);
    }
}

Output:


                                                                     
                              

This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.