{"id":1358,"date":"2022-03-03T07:26:51","date_gmt":"2022-03-03T07:26:51","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1358"},"modified":"2022-03-03T07:26:51","modified_gmt":"2022-03-03T07:26:51","slug":"display-prime-numbers-between-two-intervals","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/display-prime-numbers-between-two-intervals\/","title":{"rendered":"Display Prime Numbers Between Two Intervals"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Display Prime Numbers Between two Intervals<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Prime {\n\n    public static void main(String&#91;] args) {\n\n        int low = 20, high = 50;\n\n        while (low &lt; high) {\n            boolean flag = false;\n\n            for(int i = 2; i &lt;= low\/2; ++i) {\n                \/\/ condition for nonprime number\n                if(low % i == 0) {\n                    flag = true;\n                    break;\n                }\n            }\n\n            if (!flag &amp;&amp; low != 0 &amp;&amp; low != 1)\n                System.out.print(low + \" \");\n\n            ++low;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>23 29 31 37 41 43 47 <\/samp><\/code><\/pre>\n\n\n\n<p>In this program, each number between low and high are tested for prime. The inner for loop checks whether the number is prime or not.<\/p>\n\n\n\n<p>The difference between checking a single prime number compared to an interval is, you need to reset the value of&nbsp;<code>flag = false<\/code>&nbsp;on each iteration of the while loop.<\/p>\n\n\n\n<p><strong>Note<\/strong>: If you check the interval from&nbsp;<strong>0<\/strong>&nbsp;to&nbsp;<strong>10<\/strong>. Then, you need to exclude&nbsp;<strong>0<\/strong>&nbsp;and&nbsp;<strong>1<\/strong>. As&nbsp;<strong>0<\/strong>&nbsp;and&nbsp;<strong>1<\/strong>&nbsp;are not prime numbers. The condition will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (!flag &amp;&amp; low != 0 &amp;&amp; low != 1)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Display Prime Numbers Between two Intervals Output In this program, each number between low and high are tested for prime. The inner for loop checks whether the number is prime or not. The difference between checking a single prime number compared to an interval is, you need to reset the value of&nbsp;flag = false&nbsp;on each [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[490],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1358"}],"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=1358"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1358\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}