{"id":93,"date":"2025-02-05T11:21:26","date_gmt":"2025-02-05T11:21:26","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=93"},"modified":"2025-02-05T11:21:26","modified_gmt":"2025-02-05T11:21:26","slug":"calculate-power-using-c-language","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2025\/02\/05\/calculate-power-using-c-language\/","title":{"rendered":"Calculate Power using C Language"},"content":{"rendered":"\n<p>Write a C program that accepts two numbers x and y from input device. Calculate x power y.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>For Loop<\/strong>\n--------\n#include &lt;stdio.h>\n#include &lt;conio.h>\nvoid main() {\n    int x, y;\n    long long result = 1; \/\/ Use long long to handle larger results\n\n    \/\/ Accepting the base (x) and exponent (y) from the user\n    printf(\"Enter the base (x): \");\n    scanf(\"%d\", &amp;x);\n    printf(\"Enter the exponent (y): \");\n    scanf(\"%d\", &amp;y);\n\n    \/\/ Calculating x^y using a for loop\n    for (int i = 1; i &lt;= y; i++) {\n        result *= x; \/\/ Multiply result by x, y times\n    }\n\n    \/\/ Displaying the result\n    printf(\"%d raised to the power of %d is: %lld\\n\", x, y, result);\n\n    getch();\n}\n\n<strong>Do While<\/strong>\n---------\n#include &lt;stdio.h>\n#include &lt;conio.h>\nvoid main() {\n    int x, y;\n    long long result = 1; \/\/ Use long long to handle larger results\n\n    \/\/ Accepting the base (x) and power (y) from the user\n    printf(\"Enter the base (x): \");\n    scanf(\"%d\", &amp;x);\n    printf(\"Enter the power (y): \");\n    scanf(\"%d\", &amp;y);\n\n    \/\/ Calculating x^y using a do-while loop\n    if (y >= 0) {\n        int i = 1;\n        do {\n            result *= x; \/\/ Multiply result by x, y times\n            i++;\n        } while (i &lt;= y);\n    } else {\n        printf(\"power (y) must be a non-negative integer.\\n\");\n        return 1; \/\/ Exit the program with an error code\n    }\n\n    \/\/ Displaying the result\n    printf(\"%d raised to the power of %d is: %lld\\n\", x, y, result);\n\n    getch();\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a C program that accepts two numbers x and y from input device. Calculate x power y.<\/p>\n","protected":false},"author":1,"featured_media":82,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,23],"tags":[25,26,24],"class_list":["post-93","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bca","category-c-language","tag-bca","tag-bca-paper-solution","tag-c-language"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions\/94"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/82"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}