# C Program To Calculate the Power of a Number

In this post, we will learn how to calculate the power of a number using the C Programming language.

This program will take the value of the base and exponent from the user, then calculate the power using a logic.

For Example: Suppose Base is 8.
Exponent is 3
Then, the power will be 8*3 = 8 * 8 * 8 = 512.

So, without further ado, letâ€™s begin the tutorial.

## C Program To Calculate the Power of a Number

```// C Program To Calculate the Power of a Number
#include <stdio.h>
int main(){
int base, exponent;
long double result = 1.0;

printf("Enter a base Number: ");
scanf("%d", &base);

printf("Enter an exponent value: ");
scanf("%d", &exponent);

// logic
while (exponent != 0){
result *= base;
--exponent;
}
printf("The Result is %.2Lf", result);
return 0;
}
```

Output

``````Enter a base Number: 8
Enter an exponent value: 3
The Result is 512.00
``````

## How Does This Program Work ?

``````    int base, exponent;
long double result = 1.0;
``````

In this program, we have declared two int data types variables and one long double data type variable names as base, exponent and result respectively.

``````    // Asking for Input
printf("Enter a base Number: ");
scanf("%d", &base);

printf("Enter an exponent value: ");
scanf("%d", &exponent);
``````

Then, the user is asked to enter the values of base and exponent.Â

``````    // logic
while (exponent != 0){
result *= base;
--exponent;
}
``````

while loop is used to calculate the power of the number.

``    printf("The Result is %.2Lf", result);``

Finally, the power which is calculated above is displayed as output with the help of printf() function.

Here we have used %.2Lf because we want to show the value only till 2 decimal places. %Lf is the format specifier of long double data type variables.

Some of the used terms are as follow:

#include <stdio.h> – 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.Â

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.

math.h is a header file in the standard library of the c programming language designed for basic mathematical operations.

Int main() – 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.

scanf() – scanf() function is used to take input from the user.

printf() – printf() function is used to display and print the string under the quotation to the screen.

while Loop – In the while loop, the statements inside the body of the while loop keeps executing until it is evaluated to False.

// – Used for Commenting in C.Â

## C Program To Calculate the Power of a Number Using pow() function

```// C Program To Calculate the Power of a Number
#include <stdio.h>
#include <math.h>
int main(){
double base, exponent, result;

printf("Enter a base number: ");
scanf("%lf", &base);

printf("Enter an exponent value: ");
scanf("%lf", &exponent);

// Calculates the Power
result = pow(base, exponent);
printf("The Result of %.1lf^%.1lf is %.2lf", base, exponent, result);

return 0;
}
```

Output

``````Enter a base number: 5
Enter an exponent value: 3
The Result of 5.0^3.0 is 125.00
``````

pow() function is used to calculate the power raised to the base value.

## Conclusion

I hope after going through this post, you understand how to calculate the power of a number using C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.