Power Function Implementation

Last Updated : 4 Apr, 2026

Given two numbers b(base) and e(exponent), calculate the value of be.

Examples: 

Input: b = 3.00000, e = 5
Output: 243.00000

Input: b = 0.55000, e = 3
Output: 0.16638

Input: b = -0.67000, e = -7
Output: -16.49971

Try it on GfG Practice
redirect icon

[Naive Approach 1] Using Iteration - O(e) Time and O(1) Space

The idea is to simply multiply b exactly e times using a iterative loop.

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

double power(double b, int e) {
  
    // Initialize result to 1
    double pow = 1;

    // Multiply x for n times
    for (int i = 0; i < abs(e); i++) 
        pow = pow * b;
  	
  	if (e < 0)
      	return 1/pow;

    return pow;
}

int main() {
    double b = 3.0;
    int e = 5;
    double res = power(b, e);
    cout << res;
    return 0;
}
C
#include <stdio.h>
#include <math.h>

double power(double b, int e) {
  
    // Initialize result to 1
    double pow = 1;

    // Multiply b for e times
    for (int i = 0; i < abs(e); i++) 
        pow = pow * b;

    if (e < 0)
        return 1 / pow;

    return pow;
}

int main() {
    double b = 3.0;
    int e = 5;
    double res = power(b, e);
    printf("%f", res);
    return 0;
}
Java
class GfG {

    static double power(double b, int e) {
        
        // Initialize result to 1
        double pow = 1;

        // Multiply b for e times
        for (int i = 0; i < Math.abs(e); i++) 
            pow = pow * b;

        if (e < 0)
            return 1 / pow;

        return pow;
    }

    public static void main(String[] args) {
        double b = 3.0;
        int e = 5;
        double res = power(b, e);
        System.out.println(res);
    }
}
Python
def power(b, e):

    # Initialize result to 1
    pow = 1

    # Multiply b for e times
    for i in range(abs(e)):
        pow = pow * b

    if e < 0:
        return 1 / pow

    return pow


if __name__ == "__main__":
    b = 3.0
    e = 5
    res = power(b, e)
    print(res)
C#
using System;

class GFG {

    // Naive iterative solution to calculate pow(b, e)
    static double power(double b, int e) {
        
        // Initialize result to 1
        double pow = 1;

        // Multiply b for e times
        for (int i = 0; i < Math.Abs(e); i++) 
            pow = pow * b;

        if (e < 0)
            return 1 / pow;

        return pow;
    }

    static void Main(string[] args) {
        double b = 3.0;
        int e = 5;
        double res = power(b, e);
        Console.WriteLine(res);
    }
}
JavaScript
function power(b, e)
{

    // Initialize result to 1
    let pow = 1;

    // Multiply b for e times
    for (let i = 0; i < Math.abs(e); i++)
        pow = pow * b;

    if (e < 0)
        return 1 / pow;

    return pow;
}

// Driver Code
const b = 3.0;
const e = 5;
const res = power(b, e);
console.log(res);

Output
243

[Naive Approach 2] Using Recursion - O(e) Time and O(e) Space

The idea is to recursively multiply b exactly e times. To do so, define a recursive function that return b, if e > 0 else returns 1.

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

double power(double b, int e) {
  
    // Base Case: pow(b, 0) = 1
    if (e == 0)
        return 1;
  
    if (e < 0)
        return 1 / power(b, -e);
  
    // For all other cases
    return b * power(b, e - 1);
}

int main() {
    double b = 3.0;
    int e = 5;
    double res = power(b, e);
    cout << res;
    return 0;
}
C
#include <stdio.h>

double power(double b, int e) {
  
    // Base Case: pow(b, 0) = 1
    if (e == 0)
        return 1;
  
    if (e < 0)
        return 1 / power(b, -e);
  
    // For all other cases
    return b * power(b, e - 1);
}

int main() {
    double b = 3.0;
    int e = 5;
    double res = power(b, e);
    printf("%f", res);
    return 0;
}
Java
class GfG {

    static double power(double b, int e) {
        
        // Base Case: pow(b, 0) = 1
        if (e == 0)
            return 1;
        
        if (e < 0)
            return 1 / power(b, -e);
        
        // For all other cases
        return b * power(b, e - 1);
    }

    public static void main(String[] args) {
        double b = 3.0;
        int e = 5;
        double res = power(b, e);
        System.out.println(res);
    }
}
Python
def power(b, e):

    # Base Case: pow(b, 0) = 1
    if e == 0:
        return 1

    if e < 0:
        return 1 / power(b, -e)

    # For all other cases
    return b * power(b, e - 1)


if __name__ == "__main__":
    b = 3.0
    e = 5
    res = power(b, e)
    print(res)
C#
using System;

class GfG {

    static double power(double b, int e) {
        
        // Base Case: pow(b, 0) = 1
        if (e == 0)
            return 1;
        
        if (e < 0)
            return 1 / power(b, -e);
        
        // For all other cases
        return b * power(b, e - 1);
    }

    static void Main(string[] args) {
        double b = 3.0;
        int e = 5;
        double res = power(b, e);
        Console.WriteLine(res);
    }
}
JavaScript
function power(b, e) {
    
    // Base Case: pow(b, 0) = 1
    if (e == 0)
        return 1;
    
    if (e < 0)
        return 1 / power(b, -e);
    
    // For all other cases
    return b * power(b, e - 1);
}

// Driver Code
const b = 3.0;
const e = 5;
const res = power(b, e);
console.log(res);

Output
243

[Expected Approach] Using Divide and Conquer - O(log e) Time and O(log e) Space

The idea is to use Divide and Conquer and recursively bisect e in two equal parts. There are two possible cases:

  • If e is even: power(b, e) = power(b, e / 2) * power(b, e / 2);
  • If e is odd: power(b, e) = b * power(b, e / 2) * power(b, e / 2);
C++
using namespace std;

double power(double b, int e)
{

    // Base Case: pow(b, 0) = 1
    if (e == 0)
        return 1;

    // If exponent is negative, use the reciprocal rule: b^(-e) = 1 / b^e
    if (e < 0)
        return 1 / power(b, -e);

    // Recursively calculate power for half the exponent
    double temp = power(b, e / 2);

    // If exponent is even: b^e = (b^(e/2))^2
    if (e % 2 == 0)
        return temp * temp;
    else
        // If exponent is odd: b^e = b * (b^(e/2))^2
        return b * temp * temp;
}

int main()
{
    double b = 3.0;
    int e = 5;
    double res = power(b, e);
    cout << res;
    return 0;
}
C
#include <stdio.h>

// Recursive function to calculate pow(b, e)
double power(double b, int e)
{

    // Base Case: pow(b, 0) = 1
    if (e == 0)
        return 1;

    // If exponent is negative, use the reciprocal rule: b^(-e) = 1 / b^e
    if (e < 0)
        return 1 / power(b, -e);

    // Recursively calculate power for half the exponent
    double temp = power(b, e / 2);

    // If exponent is even: b^e = (b^(e/2))^2
    if (e % 2 == 0)
        return temp * temp;
    else
        // If exponent is odd: b^e = b * (b^(e/2))^2
        return b * temp * temp;
}

int main()
{
    double b = 3.0;
    int e = 5;
    double res = power(b, e);
    printf("%lf", res);
    return 0;
}
Java
public class GFG {

    // Recursive function to calculate pow(b, e)
    static double power(double b, int e)
    {

        // Base Case: pow(b, 0) = 1
        if (e == 0)
            return 1;

        // If exponent is negative, use the reciprocal rule:
        // b^(-e) = 1 / b^e
        if (e < 0)
            return 1 / power(b, -e);

        // Recursively calculate power for half the exponent
        double temp = power(b, e / 2);

        // If exponent is even: b^e = (b^(e/2))^2
        if (e % 2 == 0)
            return temp * temp;
        else
            // If exponent is odd: b^e = b * (b^(e/2))^2
            return b * temp * temp;
    }

    public static void main(String[] args)
    {
        double b = 3.0;
        int e = 5;
        double res = power(b, e);
        System.out.println(res);
    }
}
Python
# Recursive function to calculate pow(b, e)
def power(b, e):

    # Base Case: pow(b, 0) = 1
    if e == 0:
        return 1
    # If exponent is negative, use the reciprocal rule: b^(-e) = 1 / b^e
    if e < 0:
        return 1 / power(b, -e)

    # Recursively calculate power for half the exponent
    temp = power(b, e // 2)

    # If exponent is even: b^e = (b^(e/2))^2
    if e % 2 == 0:
        return temp * temp
    else:

        # If exponent is odd: b^e = b * (b^(e/2))^2
        return b * temp * temp


if __name__ == "__main__":
    b = 3.0
    e = 5
    res = power(b, e)
    print(res)
C#
using System;
class GFG {

    // Recursive function to calculate pow(b, e)
    static double power(double b, int e)
    {

        // Base Case: pow(b, 0) = 1
        if (e == 0)
            return 1;

        // If exponent is negative, use the reciprocal rule:
        // b^(-e) = 1 / b^e
        if (e < 0)
            return 1 / power(b, -e);

        // Recursively calculate power for half the exponent
        double temp = power(b, e / 2);

        // If exponent is even: b^e = (b^(e/2))^2
        if (e % 2 == 0)
            return temp * temp;
        else
            // If exponent is odd: b^e = b * (b^(e/2))^2
            return b * temp * temp;
    }

    static void Main(string[] args)
    {
        double b = 3.0;
        int e = 5;
        double res = power(b, e);
        Console.WriteLine(res);
    }
}
JavaScript
// Recursive function to calculate pow(b, e)
function power(b, e)
{

    // Base Case: pow(b, 0) = 1
    if (e == 0)
        return 1;

    // If exponent is negative, use the reciprocal rule:
    // b^(-e) = 1 / b^e
    if (e < 0)
        return 1 / power(b, -e);

    // Recursively calculate power for half the exponent
    let temp = power(b, Math.floor(e / 2));

    // If exponent is even: b^e = (b^(e/2))^2
    if (e % 2 == 0)
        return temp * temp;
    else

        // If exponent is odd: b^e = b * (b^(e/2))^2
        return b * temp * temp;
}

// Driver Code
const b = 3.0;
const e = 5;
const res = power(b, e);
console.log(res);

Output
243

Using Inbuilt Functions - O(log e) Time and O(1) Space

The idea is to use inbuilt functions or operators provided by programming languages to calculate be efficiently:

C++: pow(b, e)
Python: pow(b, e) or b ** e
Java: Math.pow(b, e)
C#: Math.Pow(b, e)
JavaScript: Math.pow(b, e) or b ** e

These methods are optimized for speed and handle negative exponents and floating-point numbers automatically.

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

double power(double b, int e) {  
    return pow(b, e);
}

int main() {
    double b = 3.0;
    int e = 5;
    cout << power(b, e);
}
Java
class GFG {
    static double power(double b, int e) {
        return Math.pow(b, e);
    }

    public static void main(String[] args) {
        double b = 3.0;
        int e = 5;
        System.out.println(power(b, e));
    }
}
Python
def power(b, e):

    # using (**) operator
    # return b**e

    # Return type of pow()
    # function is double
    return pow(b, e)


if __name__ == "__main__":
    b = 3.0
    e = 5
    print(power(b, e))
C#
using System;

class GFG {

    static double power(double b, int e) {

        // Math.pow() is a function that
        // return floating number
        return Math.Pow(b, e);
    }
  
    static void Main() {
        double b = 3.0;
        int e = 5;
        Console.WriteLine(power(b, e));
    }
}
JavaScript
function power(b, e) {

    // Math.pow() is a function that
    // return floating number
    return parseInt(Math.pow(b, e));
}

// Driver Code
const b = 3.0;
const e = 5;
console.log(power(b, e));

Output
243
Comment