C program to find power of a number
C program to find the power of a number. Write a C program to enter the base and exponents and calculate the power of a number using the pow function.
Write a C program to input base and exponent and calculate the power of the number using pow()
function.
To calculate power of a number we can use pow()
function which is available under math.h
header file.
This problem can also be solved using loops if we don't want to use pow()
function but we will see this later in loops section.
Example -
Input:
Enter base: 5
Enter exponent: 2
Output:
5.00 to the power 2.00 = 25.00
C program to find power of a number
#include <stdio.h>
#include <math.h>
void main()
{
double base, exponent, power;
printf("Enter base: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%lf", &exponent);
power = pow(base, exponent);
printf("%0.2lf to the power %0.2lf = %0.2lf", base, exponent, power);
}
Output
Enter base: 5
Enter exponent: 2
5.00 to the power 2.00 = 25.00
Here, double
data type is used to store data and calculate power of the number because the output could be a large number and pow()
returns double
type of data.
0.2lf
is used to print values upto 2 decimal places.
We can also find power a number using loop
Find power a number using loop