C++ program to the Power of a number

Program to find the Power of a number

To calculate the power of a number we need the two inputs – base and exponent. These two inputs are passed into a function, pow(input1,input2), as parameters and it will return the result . The function is present in the math.h header file.

For Example: Base5 ,  Exponent 3

pow(Base, Exponent) = pow(5, 3)= 125

Program to find power of a number


  1. Take  two inputs from user
  2. Inputs are stored in two double type variable say base & exp.
  3. A function, pow(base, exp) is called with base and exp as parameters.
  4. The calculated power is stored in another variable say res.
  5. res is printed with precision set up to 2 decimal places.

C++ Code:-

    //C++ Program
    //Power of a number
    #include <iomanip>
    #include <iostream>
    #include <math.h>
    using namespace std;
    //main program
    int main() 
        double expbase;
        cout<<“Enter base: “;
        //user input 1
        cout<<“Enter Exponent: “;
        //user input 2
        //calculating power using function
        double res = pow(baseexp);
        //printing result
        cout << base << “^” << exp << ” = “ ;
        cout << fixed <<setprecision(2)<<res<<endl;
        return 0;


    Enter base: 123
    Enter Exponent: 3.5
    123^3.5 = 20638013.40