C Program to Find Power of a Number using For | While | Function | Recursion | pow()
C program to find power of a number using for loop, while loop, function, recursion, pow function, and without using pow function.
In this article, you will learn how to make the C program to find power of a number using for loop, while loop, function, recursion, pow function, and without using pow function.
Example
INPUT Base Number:: 3
INPUT Exponent Number:: 4
OUTPUT:: 3^4 = 81
You should have knowledge of the following topics in C programming to understand these programs:
- C
main()function - C
printf()function - C
forloop statement - C
whileloop statement - C
Functions - C
Recursion
C Program to Find Power of a Number using For loop
- Create two variables named
bandxwith an integer data type and one variable namedrwith the long integer data type. - Assign input values in this b and x.
- Iterate the base value by using
forloop to make multiplication of the base value with itself N times. - Print the final output of the program by using the
rvariable.
// C Program to Find Power of a Number using For loop
#include <stdio.h>
// It's the driver function
int main() {
int b, x, i;
long long r = 1;
// b - Base value, x - Exponent value, r - Result value
printf("INPUT Base Number:: ");
scanf("%d", &b);
printf("\nINPUT Exponent Number:: ");
scanf("%d", &x);
// It will calculate the power of base number
for (i = x; i > 0; i--) {
r *= b;
}
// It will print the final output
printf("\n\nOUTPUT:: %d^%d = %lld\n", b, x, r);
return 0;
}
Output
INPUT Base Number:: 3
INPUT Exponent Number:: 4
OUTPUT:: 3^4 = 81
Explanation
In this given program, we have taken the inputs base value 3 and exponent value 4 from the user via the system console. After that, we pass the multiplication of the base with itself 4 times by using the for loop statement.
After the whole process, this will return the 81 the output of the given program.
C Program to Find Power of a Number using While loop
- Create two variables named
bandxwith an integer data type and one variable namedrwith the long integer data type. - Assign input values in this b and x.
- Iterate the base value by using
whileloop to make multiplication of the base value with itself N times. - Print the final output of the program by using the
rvariable.
// C Program to Find Power of a Number using While loop
#include <stdio.h>
// It's the driver function
int main() {
int b, x;
long long r = 1;
// b - Base value, x - Exponent value, r - Result value
printf("INPUT Base Number:: ");
scanf("%d", &b);
printf("\nINPUT Exponent Number:: ");
scanf("%d", &x);
// It will calculate the power of the base number
while (x--) {
r *= b;
}
// It will print the final output
printf("\n\nOUTPUT:: %d^%d = %lld\n", b, x, r);
return 0;
}
C Program to Find Power of a Number using Function
- Create two variables named
bandxwith an integer data type. - Assign input values in this b and x.
- Create a function named
FindPow()with two arguments base value and exponent value. - Iterate the base value by using
whileloop to make a multiplication of the base value with itself N times inside of the program. - Return the output value at end of this function.
- Call this function at end of the main program to print the final output of the program.
// C Program to Find Power of a Number using Function
#include <stdio.h>
// It will calculate the power of the base number
long long FindPow(int b, int e) {
long long r=1;
while (e--) {
r *= b;
}
return r;
}
// It's the driver function
int main() {
int b, x; // b - Base value, x - Exponent value
printf("INPUT Base Number:: ");
scanf("%d", &b);
printf("\nINPUT Exponent Number:: ");
scanf("%d", &x);
// It will print the final output
printf("\n\nOUTPUT:: %d^%d = %lld\n", b, x, FindPow(b, x));
return 0;
}
Output
INPUT Base Number:: 4
INPUT Exponent Number:: 5
OUTPUT:: 4^5 = 1024
C Program to Find Power of a Number using Recursion
- Create two variables named
bandxwith an integer data type. - Assign input values in this b and x.
- Create a recursive function named
FindPow()with three arguments base value, exponent value, and result value. - Call the recursive function inside of this to make a multiplication of base value with itself N times.
- Return the output value at end of the recursive function.
- Call this recursive function at end of the main program to print the final output of the program.
// C Program to Find Power of a Number using Recursion
#include <stdio.h>
// This recursion function will calculate the power of the base number
long long FindPow(int b, int e, long long r) {
if (e > 0) {
e--;
r *= b;
FindPow(b, e, r);
} else
return r;
}
// It's the driver function
int main() {
int b, x; // b - Base value, x - Exponent value
printf("INPUT Base Number:: ");
scanf("%d", &b);
printf("\nINPUT Exponent Number:: ");
scanf("%d", &x);
// It will print the final output
printf("\n\nOUTPUT:: %d^%d = %lld\n", b, x, FindPow(b, x, 1));
return 0;
}
C Program to Find Power of a Number using pow() Function
- Create three variables named
b,x, andrwith a double data type. - Assign input values in this b and x.
- Apply the
pow()function and assign its value in the r variable. - Print the final output.
// C Program to Find Power of a Number using pow() Function
#include <stdio.h>
#include <math.h>
// It's the driver function
int main() {
double b, x, r;
// b - Base value, x - Exponent value, r - Result value
printf("INPUT Base Number:: ");
scanf("%lf", &b);
printf("\nINPUT Exponent Number:: ");
scanf("%lf", &x);
// It will calculate the power of the base number
r = (double) pow(b, x);
// It will print the final output
printf("\n\nOUTPUT:: %lf ^ %lf = %lf\n", b, x, r);
return 0;
}
Output
INPUT Base Number:: 4.6
INPUT Exponent Number:: 3
OUTPUT:: 4.600000 ^ 3.000000 = 97.336000