{"id":1404,"date":"2022-03-03T19:27:20","date_gmt":"2022-03-03T19:27:20","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1404"},"modified":"2022-03-03T19:27:20","modified_gmt":"2022-03-03T19:27:20","slug":"program-to-display-first-100-prime-numbers","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-display-first-100-prime-numbers\/","title":{"rendered":"Program to display first 100 prime numbers"},"content":{"rendered":"\n<p><strong>Program to display first n prime numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\nclass PrimeNumberDemo\n{\n   public static void main(String args&#91;])\n   {\n      int n;\n      int status = 1;\n      int num = 3;\n      \/\/For capturing the value of n\n      Scanner scanner = new Scanner(System.in);\n      System.out.println(\"Enter the value of n:\");\n      \/\/The entered value is stored in the var n\n      n = scanner.nextInt();\n      if (n &gt;= 1)\n      {\n         System.out.println(\"First \"+n+\" prime numbers are:\");\n         \/\/2 is a known prime number\n         System.out.println(2);\n      }\n\n      for ( int i = 2 ; i &lt;=n ;  )\n      {\n         for ( int j = 2 ; j &lt;= Math.sqrt(num) ; j++ )\n         {\n            if ( num%j == 0 )\n            {\n               status = 0;\n               break;\n            }\n         }\n         if ( status != 0 )\n         {\n            System.out.println(num);\n            i++;\n         }\n         status = 1;\n         num++;\n      }         \n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the value of n:\n15\nFirst 15 prime numbers are:\n2\n3\n5\n7\n11\n13\n17\n19\n23\n29\n31\n37\n41\n43\n47<\/code><\/pre>\n\n\n\n<p><strong>Program to display first 100 prime numbers<\/strong><br>To display the first 100 prime numbers, you can&nbsp;<strong>either enter n value as 100 in the above program<\/strong>&nbsp;OR write a program like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class PrimeNumberDemo\n{\n   public static void main(String args&#91;])\n   {\n      int n;\n      int status = 1;\n      int num = 3;\n      System.out.println(\"First 100 prime numbers are:\");   \n      System.out.println(2);\n      for ( int i = 2 ; i &lt;=100 ;  )\n      {\n         for ( int j = 2 ; j &lt;= Math.sqrt(num) ; j++ )\n         {\n            if ( num%j == 0 )\n            {\n               status = 0;\n               break;\n            }\n         }\n         if ( status != 0 )\n         {\n            System.out.println(num);\n            i++;\n         }\n         status = 1;\n         num++;\n      }         \n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>First 100 prime numbers are:\n2\n3\n5\n7\n11\n13\n17\n19\n23\n29\n31\n37\n41\n43\n47\n53\n59\n61\n67\n71\n73\n79\n83\n89\n97\n101\n103\n107\n109\n113\n127\n131\n137\n139\n149\n151\n157\n163\n167\n173\n179\n181\n191\n193\n197\n199\n211\n223\n227\n229\n233\n239\n241\n251\n257\n263\n269\n271\n277\n281\n283\n293\n307\n311\n313\n317\n331\n337\n347\n349\n353\n359\n367\n373\n379\n383\n389\n397\n401\n409\n419\n421\n431\n433\n439\n443\n449\n457\n461\n463\n467\n479\n487\n491\n499\n503\n509\n521\n523\n541<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Program to display first n prime numbers Output: Program to display first 100 prime numbersTo display the first 100 prime numbers, you can&nbsp;either enter n value as 100 in the above program&nbsp;OR write a program like this: Output:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[537],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1404"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1404"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1404\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}