{"id":22556,"date":"2017-05-07T13:53:57","date_gmt":"2017-05-07T13:53:57","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=22556"},"modified":"2024-11-16T14:57:04","modified_gmt":"2024-11-16T14:57:04","slug":"java-program-to-check-whether-the-number-is-a-prime-number","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-program-to-check-whether-the-number-is-a-prime-number\/","title":{"rendered":"Java Program to check Prime number"},"content":{"rendered":"<h1>Overview<\/h1>\n<p>Let&#8217;s develop a Java program to check whether the number is a prime number or not in this tutorial. A prime number is a number that has no factors other than 1 and itself.<\/p>\n<h2>Java Program<\/h2>\n<p>Install and launch Java IDE. For example, Eclipse.<\/p>\n<p>Create a Java class PrimeNumber<\/p>\n<p>Add the Java code to the class.<\/p>\n<div style=\"background-color: #ffffff; padding: 0px 0px 0px 2px;\">\n<div style=\"color: #000000; background-color: #ffffff; font-family: 'Consolas'; font-size: 18pt; white-space: pre;\">\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">package<\/span><span style=\"color: #000000;\"> com.testingdocs.programs;<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">import<\/span><span style=\"color: #000000;\"> java.util.Scanner;<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #3f5fbf;\">\/*********************************************<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #3f5fbf;\"> * Program <\/span><span style=\"color: #7f7f9f;\">&#8211;<\/span><span style=\"color: #3f5fbf;\"> PrimeNumber.java <\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #3f5fbf;\"> Program Description:<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #3f5fbf;\"> * Java Program to check whether a given number<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #3f5fbf;\"> * is a prime or not<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #3f5fbf;\"> * <\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #3f5fbf;\"> * Java Tutorials <\/span><span style=\"color: #7f7f9f;\">&#8211;<\/span><span style=\"color: #3f5fbf;\"> www.TestingDocs.com<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #3f5fbf;\"> ********************************************\/<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">public<\/span> <span style=\"color: #7f0055; font-weight: bold;\">class<\/span><span style=\"color: #000000;\"> PrimeNumber {<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">public<\/span> <span style=\"color: #7f0055; font-weight: bold;\">static<\/span> <span style=\"color: #7f0055; font-weight: bold;\">void<\/span><span style=\"color: #000000;\"> main(String[] <\/span><span style=\"color: #6a3e3e;\">args<\/span><span style=\"color: #000000;\">) {<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">try<\/span><span style=\"color: #000000;\"> {<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> Scanner <\/span><span style=\"color: #6a3e3e; text-decoration: underline; text-decoration-color: #f4c82d; text-decoration-style: wavy;\">keyboard<\/span><span style=\"color: #000000;\"> = <\/span><span style=\"color: #7f0055; font-weight: bold;\">new<\/span><span style=\"color: #000000;\"> Scanner(System.<\/span><span style=\"color: #0000c0; font-style: italic; font-weight: bold;\">in<\/span><span style=\"color: #000000;\">);<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> System.<\/span><span style=\"color: #0000c0; font-style: italic; font-weight: bold;\">out<\/span><span style=\"color: #000000;\">.println(<\/span><span style=\"color: #2a00ff;\">&#8220;Enter a number :=&#8221;<\/span><span style=\"color: #000000;\">);<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">int<\/span> <span style=\"color: #6a3e3e;\">n<\/span><span style=\"color: #000000;\"> = Integer.<\/span><span style=\"color: #000000; font-style: italic;\">parseInt<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #6a3e3e;\">keyboard<\/span><span style=\"color: #000000;\">.nextLine());<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> Boolean <\/span><span style=\"color: #6a3e3e;\">primeFlag<\/span><span style=\"color: #000000;\"> = <\/span><span style=\"color: #7f0055; font-weight: bold;\">true<\/span><span style=\"color: #000000;\">;<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">for<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #7f0055; font-weight: bold;\">int<\/span> <span style=\"color: #6a3e3e;\">i<\/span><span style=\"color: #000000;\"> = 2; <\/span><span style=\"color: #6a3e3e;\">i<\/span><span style=\"color: #000000;\"> &lt;= <\/span><span style=\"color: #6a3e3e;\">n<\/span><span style=\"color: #000000;\"> \/ 2; <\/span><span style=\"color: #6a3e3e;\">i<\/span><span style=\"color: #000000;\">++) {<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #6a3e3e;\">n<\/span><span style=\"color: #000000;\"> % <\/span><span style=\"color: #6a3e3e;\">i<\/span><span style=\"color: #000000;\"> == 0) {<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #6a3e3e;\">primeFlag<\/span><span style=\"color: #000000;\"> = <\/span><span style=\"color: #7f0055; font-weight: bold;\">false<\/span><span style=\"color: #000000;\">;<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">break<\/span><span style=\"color: #000000;\">;<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> }<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> } <\/span><span style=\"color: #3f7f5f;\">\/\/ end of for<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #6a3e3e;\">primeFlag<\/span><span style=\"color: #000000;\">)<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> System.<\/span><span style=\"color: #0000c0; font-style: italic; font-weight: bold;\">out<\/span><span style=\"color: #000000;\">.println(<\/span><span style=\"color: #2a00ff;\">&#8220;Given Number is a <\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #2a00ff;\">Prime Number&#8221;<\/span><span style=\"color: #000000;\">);<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #7f0055; font-weight: bold;\">else<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> System.<\/span><span style=\"color: #0000c0; font-style: italic; font-weight: bold;\">out<\/span><span style=\"color: #000000;\">.println(<\/span><span style=\"color: #2a00ff;\">&#8220;Given Number is <\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #2a00ff;\">Not a Prime number&#8221;<\/span><span style=\"color: #000000;\">);<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> } <\/span><span style=\"color: #7f0055; font-weight: bold;\">catch<\/span><span style=\"color: #000000;\"> (NumberFormatException <\/span><span style=\"color: #6a3e3e;\">nfe<\/span><span style=\"color: #000000;\">) {<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> System.<\/span><span style=\"color: #0000c0; font-style: italic; font-weight: bold;\">out<\/span><span style=\"color: #000000;\">.println(<\/span><span style=\"color: #2a00ff;\">&#8221; Invalid Input.&#8221;<\/span><span style=\"color: #000000;\">);<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> }<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\"> } <\/span><span style=\"color: #3f7f5f;\">\/\/ end of main<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22561\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Program-Prime-Number.png\" alt=\"Java Program Prime Number\" width=\"1920\" height=\"1026\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Program-Prime-Number.png 1920w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Program-Prime-Number-300x160.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Program-Prime-Number-1024x547.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Program-Prime-Number-768x410.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Program-Prime-Number-1536x821.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/h2>\n<h2>Explanation<\/h2>\n<p>The prime program does the following things:<\/p>\n<ul>\n<li>The program prompts and takes the number as input from the user.<\/li>\n<li>The program runs a for loop that runs from 2 to n\/2.<\/li>\n<li>If a number in the range 2 to n\/2 can divide the number without any remainder then it breaks the loop after setting the primeFlag variable to false.<\/li>\n<li>If no number in the range 2 to n\/2 can divide the number without any reminder then the for loop terminates. The primeFlag variable\u2019s value remains true.<\/li>\n<li>The program displays a message based on the value of the primeFlag variable.<\/li>\n<\/ul>\n<h2>Sample Output<\/h2>\n<p>Let&#8217;s run the program and check the output.\u00a0 Choose the <strong>Run As &gt;&gt; Java Application<\/strong> option to run the Java program.<\/p>\n<p>Enter a number to check if it prime number or not.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22566\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Prime-Number-Java-Output.png\" alt=\"Prime Number Java Output\" width=\"1917\" height=\"1012\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Prime-Number-Java-Output.png 1917w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Prime-Number-Java-Output-300x158.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Prime-Number-Java-Output-1024x541.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Prime-Number-Java-Output-768x405.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Prime-Number-Java-Output-1536x811.png 1536w\" sizes=\"auto, (max-width: 1917px) 100vw, 1917px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div style=\"background-color: #ffffff; padding: 0px 0px 0px 2px;\">\n<div style=\"color: #000000; background-color: #ffffff; font-family: 'Consolas'; font-size: 18pt; white-space: pre;\">\n<p style=\"margin: 0;\"><span style=\"color: #000000;\">Enter a number:=<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #00c87d;\">71<\/span><\/p>\n<p style=\"margin: 0;\"><span style=\"color: #000000;\">Given Number is a Prime Number<\/span><\/p>\n<\/div>\n<\/div>\n<p>&#8212;<\/p>\n<h2>Java Tutorials<\/h2>\n<p>Java Tutorial on this website:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Let&#8217;s develop a Java program to check whether the number is a prime number or not in this tutorial. A prime number is a number that has no factors other than 1 and itself. Java Program Install and launch Java IDE. For example, Eclipse. Create a Java class PrimeNumber Add the Java code to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-22556","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/22556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=22556"}],"version-history":[{"count":12,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/22556\/revisions"}],"predecessor-version":[{"id":23196,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/22556\/revisions\/23196"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=22556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=22556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=22556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}