C++ program to find the Power of a number

Program to find the Power of a number


Here we will discuss how to find the power of a number using C++ Programming language.

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 power of the number(base). 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


Let’s see how the code works:

  • User gives two inputs.
  • Inputs are stored in two double type variable say base & exp.
  • A function, pow(base, exp) is called with base and exp as parameters.
  • The calculated power is stored in another variable say res.
  • 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 exp, base;
cout<<"Enter base: ";
//user input 1
cout<<"Enter Exponent: ";
//user input 2
//calculating power using function
double res = pow(base, exp);
//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

Please Login/Signup to comment