C++ program to find the power of any number x^y
ADVERTISEMENTS
C++ program to find the power of any number x^y. There are you will learn how to find the power of any number x^y in the C++ language.
Formula:
r = b ^ y
where:
r = result
b = base value
y = exponent value
There are two ways to implement these formulae:
- By using the default method
- Second, by using the pow() function
1. By using the default method
// C++ program to find the power of any number
#include <iostream>
using namespace std;
int main() {
int b, e, r = 1, i = 1;
// b = base
// e = exponent
// r = result
// i = looping incremental
cout << "Enter the base & exponent values::\n";
cin >> b >> e;
// finding power of base value by equiping exponent value
while(i <= e) {
r *= b;
i++;
}
// Output
cout << "\nResult:: " << b << "^" << e << " = " << r;
return 0;
}
Output:
Enter the base & exponent values::
5
7
Result:: 5^7 = 78125
2. By using the pow() function
Note: before using the pow() function you have to include cmath library in the program
// C++ program to find the power of any number by using pow() function
#include <iostream>
#include <cmath>
// to import pow() function
using namespace std;
int main() {
int b, e, r = 1;
// b = base
// e = exponent
// r = result
cout << "Enter the base & exponent values::\n";
cin >> b >> e;
// finding power of base value by equiping exponent value
r = pow(b, e);
// Output
cout << "\nResult:: " << b << "^" << e << " = " << r;
return 0;
}
Output:
Enter the base & exponent values::
5
7
Result:: 5^7 = 78125