C++ Program To Calculate the Power of a Number

Last Updated : 19 Aug, 2026

Given two integers x and n, the task is to calculate (x^n). Different approaches can be used, ranging from simple repeated multiplication to efficient binary exponentiation.

  • The naive approach multiplies the base n times.
  • Binary exponentiation reduces the time complexity to O(log n) by repeatedly squaring the base.

Example

For x = 3 and n = 10:
10 in binary = 1010
3^10
= 3^8 × 3^2

The binary representation of the exponent allows us to calculate the power using only a few multiplications.

program to calculate pow(x,n)

Approaches to Calculate the Power of a Number

1. Naive Iterative Approach

The simplest approach is to multiply x by itself n times.

C++
#include <iostream>
using namespace std;

long long power(int x, unsigned int n) {
    long long result = 1;

    for (unsigned int i = 0; i < n; i++)
        result *= x;

    return result;
}

int main() {
    int x = 2;
    unsigned int n = 3;

    cout << power(x, n);

    return 0;
}

Output
8

Explanation: The loop multiplies x by itself n times, giving (x^n).

2. Recursive Approach

The power can also be calculated recursively using the relation:

x^n = x \times x^{n-1}

The recursion stops when n becomes 0.

C++
#include <iostream>
using namespace std;

long long power(int x, unsigned int n) {
    if (n == 0)
        return 1;

    return x * power(x, n - 1);
}

int main() {
    int x = 2;
    unsigned int n = 3;

    cout << power(x, n);

    return 0;
}

Output
8

Explanation: The function keeps reducing the exponent by one until it reaches 0. The multiplication is then performed while returning from the recursive calls.

3. Divide and Conquer Approach

The recursive approach can be optimized by dividing the exponent by two at each step.

For an even exponent:

x^n = (x^{n/2})^2

For an odd exponent:

x^n = x \times (x^{n/2})^2

C++
#include <iostream>
using namespace std;

long long power(int x, unsigned int n) {
    if (n == 0)
        return 1;

    long long half = power(x, n / 2);

    if (n % 2 == 0)
        return half * half;

    return x * half * half;
}

int main() {
    int x = 2;
    unsigned int n = 3;

    cout << power(x, n);

    return 0;
}

Output
8

Explanation: The exponent is divided by two at every recursive call. The result of the smaller subproblem is calculated once and reused, reducing the number of multiplications.

4. Binary Exponentiation

Binary exponentiation uses the binary representation of the exponent. At each step, if the current least significant bit is 1, the current base is multiplied into the result. The base is then squared and the exponent is right-shifted by one bit.

C++
#include <iostream>
using namespace std;

long long power(long long x, unsigned int n) {
    long long result = 1;

    while (n > 0) {
        if (n & 1)
            result *= x;

        x *= x;
        n >>= 1;
    }

    return result;
}

int main() {
    long long x = 2;
    unsigned int n = 3;

    cout << power(x, n);

    return 0;
}

Output
8

Explanation: n & 1 checks whether the current bit of the exponent is 1. The base is squared after every iteration, while n >>= 1 moves to the next bit.

5. Using the Inbuilt pow() Function

C++ provides the pow() function in the <cmath> header to calculate powers directly.

C++
#include <cmath>
#include <iostream>
using namespace std;

int main() {
    int x = 2;
    int n = 3;

    cout << pow(x, n);

    return 0;
}

Output
8

Explanation: The pow() function calculates x raised to the power n. Its return type is floating-point, so converting the result to an integer should only be done when the expected result is known to be exactly representable.

Handling Negative Exponents and Fractional Bases

For a negative exponent, the power can be represented as:

x^{-n} = \frac{1}{x^n}

A floating-point base and signed integer exponent can therefore be handled using binary exponentiation.

C++
#include <iostream>
using namespace std;

double power(double x, int n) {
    long long exp = n;

    if (exp < 0) {
        x = 1.0 / x;
        exp = -exp;
    }

    double result = 1.0;

    while (exp > 0) {
        if (exp & 1)
            result *= x;

        x *= x;
        exp >>= 1;
    }

    return result;
}

int main() {
    double x = 2.0;
    int n = -3;

    cout << power(x, n);

    return 0;
}

Output
0.125

Explanation: For a negative exponent, the base is replaced by its reciprocal and the exponent is made positive. Binary exponentiation is then applied normally.

Note: The base must not be 0 when the exponent is negative because division by zero is undefined.

Comment