C++ Program to Calculate Power of a Number
Answers
Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float base, exponent, result;
cout << "Enter base and exponent respectively: ";
cin >> base >> exponent;
result = pow(base, exponent);
cout << base << "^" << exponent << " = " << result;
return 0;
}
Explanation: In this program, we have used the pow() function to calculate the power of a number.
Notice that we have included the cmath header file in order to use the pow() function.
We take the base and exponent from the user.
We then use the pow() function to calculate the power. The first argument is the base, and the second argument is the exponent.
Answer:
This is the required C++ program for the question.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x,y, result;
cout << "Enter base: ";
cin >> x;
cout << "Enter power: ";
cin >> y;
result = pow(x,y);
cout << "Result: "<< result;
return 0;
}
Explanation:
- The pow() function is used to calculate power of a number. For example, pow(x, y) returns x raised to the power y.
- The pow() function is present in <cmath> header file.
- To use pow() function, we have to include the <cmath> header file in the program.
- After including the header file, I have asked the user to enter base and exponent. Using pow function, x^y is calculated and displayed.
See the attachment for output ☑.