C Program to find the power of a number
It is easy to find the power of any number in calculator. For doing the same in a C program needs
an algorithm.
Algorithm
Identify the base number -> Identify the exponent -> find power.
This can be illustrated with an example of
23.
Here Base is '2'; exponent is '3' and the power is 2*2*2 = 8.
#include
int main()
{
int base, exponent;
long result = 1;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exponent);
while (exponent != 0)
{
result *= base;
--exponent;
}
printf("Answer = %ld", result);
return 0;
}
Comments
Post a Comment