{"id":11385,"date":"2020-12-25T22:46:41","date_gmt":"2020-12-25T17:16:41","guid":{"rendered":"https:\/\/java2blog.com\/?p=11385"},"modified":"2021-01-11T18:14:44","modified_gmt":"2021-01-11T12:44:44","slug":"nth-prime-number-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/nth-prime-number-java\/","title":{"rendered":"Find nth prime number in java"},"content":{"rendered":"<p>In this post, we will see how to find nth Prime number in java.<br \/>\n[toc]<\/p>\n<h2>What is prime number<\/h2>\n<p>Prime number is a number that is greater than 1 and divided by 1 or itself.<br \/>\n<strong>For example:<\/strong> 5, 7, 13, 23<\/p>\n<p>We have already seen how to <a href=\"https:\/\/java2blog.com\/java-isprime\/\" target=\"_blank\" rel=\"noopener noreferrer\">check if number is prime or not<\/a>. We will use same method to find nth prime number in java.<\/p>\n<h2>Nth prime number in java<\/h2>\n<p>Here are steps to find nth prime number in java.<\/p>\n<ul>\n<li>Take value of <code>n<\/code> from user using Scanner class.<\/li>\n<li>Intialize a variable <code>count<\/code>. It will keep track of number of prime number processed so far.<\/li>\n<li>Intialize a variable <code>i<\/code>. It will keep track of current number.<\/li>\n<li>Iterate over <a href=\"https:\/\/java2blog.com\/while-loop-java-example\/\" target=\"_blank\" rel=\"noopener noreferrer\">while loop<\/a> until <code>count ! = n<\/code>\n<ul>\n<li>Increment <code>i<\/code> by 2 as even numbers are not prime<\/li>\n<li>Check if number is prime. If number is prime then increment <code>count<\/code> by 1.<\/li>\n<\/ul>\n<\/li>\n<li>Return <code>i<\/code> which is nth prime number.<\/li>\n<\/ul>\n<pre code=\"java\">\npackage org.arpit.java2blog;\n\nimport java.util.Scanner;\n\npublic class NthPrimeNumberJava {\n\n    public static void main(String args[]){\n\n        Scanner in = new Scanner(System.in);\n\n        \/\/Input the value of 'n'\n        System.out.println(\"Enter the value of n\");\n        int n= in.nextInt();\n\n        int nthPrimeNumber = findNthPrimeNumber(n);\n        System.out.println(\"Nth Prime Number is: \"+nthPrimeNumber);\n    }\n\n    private static int findNthPrimeNumber(int n)\n    {\n        if(n==1)\n        {\n            return 2;\n        }\n\n        int i=1;\n        int count=1;\n        \/\/While loop should run until we find nth prime number i.e (count != n)\n        while(count != n){\n            \/\/ increment number by 2 as even numbers are always not prime\n            i+=2;\n\n            if(isPrime(i))\n                count++;  \/\/ increment the count when we get prime number\n        }\n        \/\/ return nth prime number\n        return i;\n    }\n\n    public static boolean isPrime(int num) {\n        if (num <= 1) {\n            return false;\n        }\n        for (int i = 2; i <= Math.sqrt(num); i++) {\n            if (num % i == 0) {\n                return false;\n            }\n        }\n        return true;\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">\nEnter the value of n<br \/>\n24<br \/>\nNth Prime Number is: 89\n<\/div>\n<p>That's all about how to find nth prime number in java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn about how to find nth prime number in java.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,68],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/11385"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=11385"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/11385\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=11385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=11385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=11385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}