C Program to Calculate the Power of a Number
Example on how to calculate the power of a number if the exponent is an integer. Also, you will learn to compute the power using pow() function.
The program below takes two integers from the user (a base number and an exponent) and calculates the power.
For example: In case of 23
- 2 is the base number
- 3 is the exponent
- And, the power is equal to 2*2*2
Example #2: Power Using pow() Function
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int base, exponent, result;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exponent);
// calculates the power
result = pow(base, exponent);
printf("%d^%d = %d", base, exponent, result);
printf("\nSubscribe My Channel");
return 0;
getch();
}
Output
Enter the value of base = 2
Enter the value of Exponent= 2
2^2 = 4
No comments: