<\/span><\/h2>\n\n\n\n\/\/ C Program To Calculate the Power of a Number\n#include <stdio.h>\nint main(){\n int base, exponent;\n long double result = 1.0;\n \n \/\/ Asking for Input\n printf(\"Enter a base Number: \");\n scanf(\"%d\", &base);\n \n printf(\"Enter an exponent value: \");\n scanf(\"%d\", &exponent);\n \n \/\/ logic\n while (exponent != 0){\n result *= base;\n --exponent;\n }\n printf(\"The Result is %.2Lf\", result);\n return 0;\n}\n<\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEnter a base Number: 8\r\nEnter an exponent value: 3\r\nThe Result is 512.00\r<\/code><\/pre>\n\n\n\n<\/span>How Does This Program Work ?<\/strong><\/span><\/h2>\n\n\n\n int base, exponent;\r\n long double result = 1.0;<\/strong>\r<\/code><\/pre>\n\n\n\nIn this program, we have declared two int data types variables and one long double data type variable names as base<\/strong>, exponent<\/strong> and result<\/strong> respectively.<\/p>\n\n\n\n \/\/ Asking for Input\r\n printf(\"Enter a base Number: \");\r\n scanf(\"%d\", &base);\r\n \r\n printf(\"Enter an exponent value: \");\r\n scanf(\"%d\", &exponent);<\/strong>\r<\/code><\/pre>\n\n\n\nThen, the user is asked to enter the values of base and exponent.\u00a0<\/p>\n\n\n\n
\/\/ logic\r\n while (exponent != 0){\r\n result *= base;\r\n --exponent;\r\n }<\/strong>\r<\/code><\/pre>\n\n\n\nwhile loop is used to calculate the power of the number.<\/p>\n\n\n\n
printf(\"The Result is %.2Lf\", result);<\/strong><\/code><\/pre>\n\n\n\nFinally, the power which is calculated above is displayed as output with the help of printf()<\/strong> function.<\/p>\n\n\n\nHere we have used %.2Lf <\/strong>because we want to show the value only till 2<\/strong> decimal places. %Lf <\/strong>is the format specifier of long double data type variables.<\/p>\n\n\n\nSome of the used terms are as follow:<\/p>\n\n\n\n
#include <stdio.h><\/strong> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.\u00a0<\/p>\n\n\n\nThe stdio.h <\/strong>is a file which contains input and output functions like scanf()<\/strong> and printf()<\/strong> to take input and display output respectively.<\/p>\n\n\n\nmath.h<\/strong> is a header file in the standard library of the c programming language designed for basic mathematical operations.<\/p>\n\n\n\nInt main()<\/strong> – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function. <\/p>\n\n\n\nscanf()<\/strong> – scanf() function is used to take input from the user. <\/p>\n\n\n\nprintf()<\/strong> – printf() function is used to display and print the string under the quotation to the screen. <\/p>\n\n\n\nwhile Loop<\/strong> – In the while loop, the statements inside the body of the while loop keeps executing until it is evaluated to False.<\/p>\n\n\n\n\/\/ <\/strong>– Used for Commenting in C.\u00a0<\/p>\n\n\n\n<\/span>C Program To Calculate the Power of a Number Using pow() function<\/strong><\/span><\/h2>\n\n\n\n\/\/ C Program To Calculate the Power of a Number\n#include <stdio.h>\n#include <math.h>\nint main(){\n double base, exponent, result;\n \n \/\/ Asking for Input\n printf(\"Enter a base number: \");\n scanf(\"%lf\", &base);\n \n printf(\"Enter an exponent value: \");\n scanf(\"%lf\", &exponent);\n \n \/\/ Calculates the Power\n result = pow(base, exponent);\n printf(\"The Result of %.1lf^%.1lf is %.2lf\", base, exponent, result);\n \n return 0;\n}\n<\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEnter a base number: 5\r\nEnter an exponent value: 3\r\nThe Result of 5.0^3.0 is 125.00<\/strong>\r<\/code><\/pre>\n\n\n\npow()<\/strong> function is used to calculate the power raised to the base value.<\/p>\n\n\n\n<\/span>Conclusion<\/strong><\/span><\/h2>\n\n\n\nI hope after going through this post, you understand how to calculate the power of a number using C Programming language.<\/p>\n\n\n\n
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.<\/p>\n","protected":false},"excerpt":{"rendered":"
In this post, we will learn how to calculate the power of a number using the C Programming language.<\/p>\n","protected":false},"author":2,"featured_media":4123,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[167,294],"tags":[222,281,282,206],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.codingbroz.com\/wp-content\/uploads\/2021\/09\/c-program-to-calculate-the-power-of-a-number-codingbroz.png","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4121"}],"collection":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/comments?post=4121"}],"version-history":[{"count":2,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4121\/revisions"}],"predecessor-version":[{"id":4124,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4121\/revisions\/4124"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media\/4123"}],"wp:attachment":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media?parent=4121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/categories?post=4121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/tags?post=4121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}