Power of a Number Program in C Language
To find the power of a number in C programming, use pow() function of cmath library, or use a loop to multiply this number iteratively power number of times.
Power of a Number using pow() Function
In the following program, we will find the power of a number using pow() function of cmath library. pow() function takes the base and exponent as arguments respectively.
main.c
#include <stdio.h> #include <math.h> int main() { int base, power; printf("Enter base : "); scanf("%d", &base); printf("Enter exponent (power) : "); scanf("%d", &power); int result = pow(base, power); printf("Result : %d\n", result); }
Output
Enter base : 4 Enter exponent (power) : 2 Result : 16 Program ended with exit code: 0
Output
Enter base : 2 Enter exponent (power) : 10 Result : 1024 Program ended with exit code: 0
ADVERTISEMENT
Power of a Number using While Loop
In the following program, we find base to the power of exponent using C While Loop.
main.c
#include <stdio.h> int main() { int base, power; printf("Enter base : "); scanf("%d", &base); printf("Enter exponent (power) : "); scanf("%d", &power); int i = 0, result = 1; while (i < power) { result = result * base; i++; } printf("Result : %d\n", result); }
Output
Enter base : 2 Enter exponent (power) : 3 Result : 8 Program ended with exit code: 0
Output
Enter base : 5 Enter exponent (power) : 2 Result : 25 Program ended with exit code: 0
Conclusion
In this C Tutorial, we learned how to find the power of a number in C programming, with example programs.