{"id":1406,"date":"2022-03-03T19:31:30","date_gmt":"2022-03-03T19:31:30","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1406"},"modified":"2022-03-03T19:31:30","modified_gmt":"2022-03-03T19:31:30","slug":"java-program-to-check-prime-number","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/java-program-to-check-prime-number\/","title":{"rendered":"Java program to check prime number"},"content":{"rendered":"\n<p>The number which is only divisible by itself and 1 is known as\u00a0<strong>prime number<\/strong>, for example 7 is a prime number because it is only divisible by itself and 1.<br>This program takes the number (entered by user) and then checks whether the input number is prime or not. The program then displays the result. If you are looking for a program that displays the prime number between two intervals then see:\u00a0Java program to display prime numbers between 1 to n.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example: Program to check whether input number is prime or not<\/h4>\n\n\n\n<p>To understand this program you should have the knowledge of\u00a0for loop,\u00a0if-else statements\u00a0and\u00a0break statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\nclass PrimeCheck\n{\n   public static void main(String args&#91;])\n   {\t\t\n\tint temp;\n\tboolean isPrime=true;\n\tScanner scan= new Scanner(System.in);\n\tSystem.out.println(\"Enter any number:\");\n\t\/\/capture the input in an integer\n\tint num=scan.nextInt();\n        scan.close();\n\tfor(int i=2;i&lt;=num\/2;i++)\n\t{\n           temp=num%i;\n\t   if(temp==0)\n\t   {\n\t      isPrime=false;\n\t      break;\n\t   }\n\t}\n\t\/\/If isPrime is true then the number is prime else not\n\tif(isPrime)\n\t   System.out.println(num + \" is a Prime Number\");\n\telse\n\t   System.out.println(num + \" is not a Prime Number\");\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter any number:\n19\n19 is a Prime Number<\/code><\/pre>\n\n\n\n<p>Output 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter any number:\n6\n6 is not a Prime Number<\/code><\/pre>\n\n\n\n<p>You can also use\u00a0while loop\u00a0to check the prime number:<br>Just replace this part of the code in above program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for(int i=2;i&lt;=num\/2;i++)\n{\n   temp=num%i;\n   if(temp==0)\n   {\n      isPrime=false;\n      break;\n   }\n}<\/code><\/pre>\n\n\n\n<p>with this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int i=2;\nwhile(i&lt;= num\/2)\n{\n   if(num % i == 0)\n   {\n\tisPrime = false;\n\tbreak;\n   }\n   i++;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The number which is only divisible by itself and 1 is known as\u00a0prime number, for example 7 is a prime number because it is only divisible by itself and 1.This program takes the number (entered by user) and then checks whether the input number is prime or not. The program then displays the result. If [&hellip;]<\/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\/1406"}],"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=1406"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1406\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}