Program to find power of any number in C++
Written by
The pow function calculates the power of a number by raising a base number to a specified exponent. The function is defined in the <math.h> library and can be used to perform efficient calculations with large exponents.
To use the pow function, you must include the <math.h> library in your code and pass in two arguments: the base and the exponent. The base is the number that you want to raise to a power, and the exponent is the power to which you want to raise the base. The function returns the result of the calculation as a double data type.
Example:
-
Input the base: 23
Input the exponent: 4
Answer: 279841
-
Input the base: 9
Input the exponent: -3
Answer: 0.00137174
# Algorithm
- Prompt the user to enter a base and an exponent.
- Store the user's input in variables
base
andexponent
. - Use the
pow
function from the<math.h>
library to calculate the power of the base raised to the exponent. Assign the result to a variableresult
. - Print the value stored in the
result
variable.
Code:
// Program to find power of any number
#include <iostream>
#include <math.h>
using namespace std;
int main() {
float base, expo;
cout << "Input the base: ";
cin >> base;
cout << "Input the exponent: ";
cin >> expo;
float result = pow(base, exponent);
cout << "Answer: " << result;
}
Output
Input the base: 2
Input the exponent: 4
Answer: 16