In this blog post, we learn how to write a C program to calculate the power of a number?. We will write the C program to calculate the power of a number using the loop and arithmetic operators. Write the C program to calculate the power of a number using pow(x,n). Here we will write C function to calculate the power of a number. How to write C program to find power of a number using recursion.
Let see an example,
Input : data = 2, n = 3 Output : 8 Input : data = 7, n = 2 Output : 49
C program to compute the power of a number:
Below C program works only if the exponent is a positive integer. The program takes two integers from the user (base number and exponent) and calculates the power using loop and multiplication operator.
#include <stdio.h>
int main()
{
int base,exponent;
long long result = 1;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
if(exponent >= 0)
{
while(exponent--)
{
result *= base;
}
printf("Result = %lld",result);
}
else
{
printf("Enter positive exponent\n");
}
return 0;
}
Output:
Enter base: 2
Enter exponent: 4
Result = 16
Code Analysis:
- After the first iteration, the value of the result will result = 1 * 2= 2.
- After the second iteration, the value of the result will result = 2 * 2= 4.
- After the third iteration, the value of the result will result = 4 * 2= 8.
- After the fourth iteration, the value of the result will result = 8 * 2= 16.
Calculating power using the recursive function in C:
Below C program used to calculate powers using the recursion. Here exponent could be positive or negative integer number.
#include<stdio.h>
float power(float base, int exponent)
{
float temp;
if( exponent == 0)
{
return 1;
}
temp = power(base, exponent/2);
if (exponent%2 == 0)
{
return temp*temp;
}
else
{
if(exponent > 0)
{
return base*temp*temp;
}
else
{
return (temp*temp)/base;
}
}
}
int main()
{
double base;
int exponent;
printf("Enter base: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
printf("%.2f^%d = %.2f", base, exponent, power(base, exponent));
return 0;
}
Output 1:
Enter base: 10
Enter exponent: 2
10.00^2 = 100.00
Output 2:
Enter base: 4
Enter exponent: -1
4.00^-1 = 0.25
Calculating power using the pow() function in C:
In the below program, we will calculate powers using the pow() function. You must include math.h header file before using the pow() function.
#include<stdio.h>
#include<math.h>
int main()
{
double base, exponent;
printf("Enter base: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%lf", &exponent);
printf("%.2f^%.2f = %.2f", base, exponent, pow(base, exponent));
return 0;
}
Output:
Enter base: 10
Enter exponent: 4
10.00^4.00 = 10000.00
Recommended Post:
- C program to calculate the power of a number.
- sqrt function in C.
- C program to find all roots of a quadratic equation using switch case.
- C program to find the roots of a quadratic equation.
- How to find whether a given number is prime number in C?
- Use of isxdigit in C programming.
- How to use ispunct function in C programming?
- tolower function in C.
- How to use the islower function in C?
- Use of iscntrl function in C.