C++ program to the Power of a number

Power of a number in C++

In this post we will see various methods to calculate Power of a Number in C++.

We need the following to calculate power of number -
Base : 5
Exponent : 3
Power = 53 = 125 
Power of Number

Methods

  1. Method 1: Using Pow(base, exp) method in math.h library
  2. Method 2: Using Iterative Loop.
  3. Method 3: Enhanced iterative loop to handle negative exponent too.
  4. Method 4: Recursive approach
Power of a Number in C++

Method 1 (with inbuilt function)

This method handles all cases, when exponent/bases are integers/decimals or positive/negative

Code

Run

// This method handles all cases, 
// when exponent/bases are integers/decimals or positive/negative
// pow function is contained in math.h library
#include<iostream>
#include<math.h>
using namespace std;

int main() 
{
    double base = 1.5;
    double expo1 = 2.5;
    double expo2 = -2.5;
    double res1, res2;
    
    // calculates the power
    res1 = pow(base, expo1);
    res2 = pow(base, expo2);
    
    cout << base << " ^ " << expo1 << " = " << res1 << endl;
    cout << base << " ^ " << expo2 << " = " << res2 << endl;
    
    return 0;
}

Output

1.5 ^ 2.5 = 2.75568
1.5 ^ -2.5 = 0.362887

Method 2 (without inbuilt function)

Sometimes, the interviewer may ask to calculate power without using the inbuilt function. We can do so in the following way but it has some trade-offs.

Only works when –

  • When exponent is positive
  • When exponent is integer

Code

Run
// Only works when exponent is positive integer
#include<iostream>
using namespace std;

int main() 
{
    double base = 1.5;
    // Works only when exponent is positive integer
    int expo = 2;
    double res = 1.0;
    
    while (expo != 0) {
        res *= base;
        expo--;
    }
    
    cout << "Result = " << res;
    
    return 0;
}

Output

Result = 2.25

Method 3 (without inbuilt function)

We solve the previous issue where code wasn’t able to handle negative exponent. But it still has the following issue, it doesn’t work when

  • When exponent is decimal

Code

Run
// Only works when exponent is integer
#include<iostream>
using namespace std;

int main() 
{
    double base = 1.5;
    int expo = -2;
    double res = 1.0;
    
    while (expo > 0) {
        res *= base;
        expo--;
    }
    
    while (expo < 0) {
        res /= base;
        expo++;
    }
    
    cout << "Result = " << res;
    
    return 0;
}

Output

Result = 0.444444

Method 4 (Using Recursion)

This method works on recursion in C++. But it still has the following issue, it doesn’t work when

  • When exponent is decimal

Code

Run
// Only works when exponent is integer
#include<iostream>
using namespace std;

double power(double base, int exp) {
    if (exp > 0)
        return (power(base, exp - 1) * base);
    else if (exp < 0)
        return (power(base, exp + 1) / base);
    else
        return 1;
}

int main() 
{
    double base = 1.5;
    int expo = -2;
    double res = 1.0;
    
    
    cout << "Result = " << power(base, expo);
    
    return 0;
}

Output

Result = 0.444444

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “C++ program to the Power of a number”