Program to find power of any number in C++

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:

  1. Input the base: 23

    Input the exponent: 4

    Answer: 279841

  2. Input the base: 9

    Input the exponent: -3

    Answer: 0.00137174

# Algorithm

  1. Prompt the user to enter a base and an exponent.
  2. Store the user's input in variables base and exponent.
  3. 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 variable result.
  4. 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

Program to find power of any number in C++