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: Base – 5 , Exponent – 3
pow(Base, Exponent) = pow(5, 3)
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.
//Power of a number
using namespace std;
double exp, base;
cout<<"Enter base: ";
//user input 1
cout<<"Enter Exponent: ";
//user input 2
//calculating power using function
double res = pow(base, exp);
cout << base << "^" << exp << " = " ;
cout << fixed <<setprecision(2)<<res<<endl;
Enter base: 123
Enter Exponent: 3.5
123^3.5 = 20638013.40