C Program to Find Power of a Number using For loop | While | Function | Recursion
C program to find power of a number using for loop, while loop, function, recursion, and do-while loop.
In this article, you will learn how to write a c program to find power of a number without using pow function.
Example
Enter the Base & Exponent values:
5
7
Result: 5^7 = 78125
You should have knowledge of the following topics in c programming to understand these programs:
- C
Math
library - C
main()
function - C
printf()
function - C loops
- C Functions
- C Recursion
Formula to find power of a number
R = B ^ Y
Where
R = Result
B = Base Value
Y = Exponent Value
In this article, we solved this problem in six methods:
- Using the for loop
- Using the while loop
- Using the function
- Using the recursion
- Using the pow() function
- Using the do-while loop
C Program to Find Power of a Number using For loop
// C Program to Find Power of a Number using For loop
#include <stdio.h>
int main() {
int base, exponent, result = 1;
printf("Enter the Base & Exponent values:\n");
scanf("%d%d", &base, &exponent);
// It will finding the power of Base value
// by equiping Exponent value
for (int i = 1; i <= exponent; i++) {
result *= base;
}
printf("\nResult: %d^%d = %d\n", base, exponent, result);
return 0;
}
Enter the Base & Exponent values:
5
7
Result: 5^7 = 78125
C Program to Find Power of a Number using While loop
// C Program to Find Power of a Number using While loop
#include <stdio.h>
int main() {
int base, exponent, result = 1, i = 1;
printf("Enter the Base & Exponent values:\n");
scanf("%d%d", &base, &exponent);
// It will finding the power of Base value
// by equiping Exponent value
while(i <= exponent) {
result *= base;
i++;
}
printf("\nResult: %d^%d = %d\n", base, exponent, result);
return 0;
}
Explanation
In this program, we have taken inputs 5 as a base
value and 7 as an exponent
value.
In the first program iterated multiplication of base value with itself 7 times
then it returned the final output.
The second program normally used maths' pow()
function, In this function, passed base value at first argument and exponent value at second argument.
C Program to Find Power of a Number using Function
// C Program to Find Power of a Number using Function
#include <stdio.h>
// This will find the power of a number
void CalculatePower(int b, int e) {
int r = 1;
// by equiping Exponent value
for (int i = 1; i <= e; i++) {
r *= b;
}
printf("\nResult: %d^%d = %d\n", b, e, r);
}
// It's the driver code
int main() {
int base, exponent;
printf("Enter the Base & Exponent values:\n");
scanf("%d%d", &base, &exponent);
CalculatePower(base, exponent);
return 0;
}
Output
Enter the Base & Exponent values:
9
5
Result: 9^5 = 59049
You should also read these things
C++ Program to Find Power of a Number
Find Power of a Number in Java
C Program to Find Power of a Number using Recursion
// C Program to Find Power of a Number using Recursion
#include <stdio.h>
// This will find the power of a number
// Using the recursion
int CalculatePower(int b, int e) {
if (e != 0)
return (b * CalculatePower(b, e - 1));
else
return 1;
}
// It's the driver code
int main() {
int base, exponent;
printf("Enter the Base & Exponent values:\n");
scanf("%d%d", &base, &exponent);
printf("\nResult: %d^%d = %d\n", base, exponent, CalculatePower(base, exponent));
return 0;
}
C Program to Find Power of a Number using pow Function
// C Program to Find Power of a Number using pow Function
#include <stdio.h>
#include <math.h>
int main() {
int base, exponent, result = 1;
printf("Enter the Base & Exponent values:\n");
scanf("%d%d", &base, &exponent);
// It will finding the power of Base value
// by equiping Exponent value
result = pow(base, exponent);
printf("\nResult: %d^%d = %d\n", base, exponent, result);
return 0;
}
Output
Enter the Base & Exponent values:
6
8
Result: 6^8 = 1679616
pow()
the function you have to include math.h
header file in the second program.C Program to Find Power of a Number using Do While loop
// C Program to Find Power of a Number using Do While loop
#include <stdio.h>
int main() {
int base, exponent, result = 1, i = 1;
printf("Enter the Base & Exponent values:\n");
scanf("%d%d", &base, &exponent);
// It will finding the power of Base value
// by equiping Exponent value
do {
result *= base;
i++;
} while(i <= exponent);
printf("\nResult: %d^%d = %d\n", base, exponent, result);
return 0;
}
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student