Raise to Power of Its Own Reverse

Last Updated : 18 Jul, 2026

Given a number n, find the value of n raised to the power of its own reverse. The result will always fit into a 32-bit signed integer.

Examples:

Input: n = 2
Output: 4
Explanation: The reverse of 2 is 2, and 22 = 4.

Input: n = 10
Output: 10
Explanation: The reverse of 10 is 1 (leading zero is discarded), and 10 raised to the power 1 is 10.

Try It Yourself
redirect icon

[Naive Approach] Iterative Multiplication - O(r) Time and O(1) Space

The idea is to first compute the reverse of the given number. Once the reversed value r is obtained, multiply n by itself exactly r times to calculate n ^ r.

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

int reverseExponentiation(int n)
{

    // Find reverse of the number
    int rev = 0;
    int temp = n;

    while (temp > 0)
    {
        rev = rev * 10 + temp % 10;
        temp /= 10;
    }

    // Compute n^rev
    int res = 1;
    for (int i = 0; i < rev; i++)
        res *= n;

    return res;
}

int main()
{
    int n = 10;

    cout << reverseExponentiation(n);

    return 0;
}
Java
public class GFG {
    // Function to reverse the number and compute n^rev
    public static int reverseExponentiation(int n)
    {
        // Find reverse of the number
        int rev = 0;
        int temp = n;

        while (temp > 0) {
            rev = rev * 10 + temp % 10;
            temp /= 10;
        }

        // Compute n^rev
        int res = 1;
        for (int i = 0; i < rev; i++)
            res *= n;

        return res;
    }

    public static void main(String[] args)
    {
        int n = 10;

        System.out.println(reverseExponentiation(n));
    }
}
Python
def reverseExponentiation(n):
    # Find reverse of the number
    rev = 0
    temp = n

    while temp > 0:
        rev = rev * 10 + temp % 10
        temp //= 10

    # Compute n^rev
    res = 1
    for _ in range(rev):
        res *= n

    return res


if __name__ == '__main__':
    n = 10

    print(reverseExponentiation(n))
C#
using System;

public class GFG {
    // Function to reverse the number and compute n^rev
    public static int reverseExponentiation(int n)
    {
        // Find reverse of the number
        int rev = 0;
        int temp = n;

        while (temp > 0) {
            rev = rev * 10 + temp % 10;
            temp /= 10;
        }

        // Compute n^rev
        int res = 1;
        for (int i = 0; i < rev; i++)
            res *= n;

        return res;
    }

    public static void Main()
    {
        int n = 10;

        Console.WriteLine(reverseExponentiation(n));
    }
}
JavaScript
function reverseExponentiation(n)
{
    // Find reverse of the number
    let rev = 0;
    let temp = n;

    while (temp > 0) {
        rev = rev * 10 + temp % 10;
        temp = Math.floor(temp / 10);
    }

    // Compute n^rev
    let res = 1;
    for (let i = 0; i < rev; i++) {
        res *= n;
    }

    return res;
}

// Driver Code
const n = 10;
console.log(reverseExponentiation(n));

Output
10

[Better Approach] Recursion – O(r) Time and O(r) Space

The idea is to first reverse the given number to obtain the exponent r. Then, recursively compute n^r by multiplying n with the result of n^(r-1). The recursion stops when the exponent becomes 0, where the answer is 1.

Working of Approach:

  • Reverse the given number to obtain the exponent rev.
  • Call the recursive function power(n, rev) to compute n raised to the power rev.
  • In each recursive call, multiply n with the result of power(n, rev - 1), reducing the exponent by 1.
  • When the exponent becomes 0, return 1, which acts as the base case of recursion.
  • As the recursive calls return, the multiplications are performed, producing the final value of n^rev.
C++
#include <iostream>
using namespace std;

// Function to reverse the given number
int revNum(int n)
{
    int rev = 0;

    while (n > 0)
    {
        rev = rev * 10 + n % 10;
        n /= 10;
    }

    return rev;
}

// Function to compute x raised to the power y
int power(int x, int y)
{
    // Base case
    if (y == 0)
        return 1;

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

// Function to return n raised to the power of its reverse
int reverseExponentiation(int n)
{
    int rev = revNum(n);
    return power(n, rev);
}

int main()
{
    int n = 10;

    cout << reverseExponentiation(n);

    return 0;
}
Java
public class GFG {
    // Function to reverse the given number
    static int revNum(int n)
    {
        int rev = 0;

        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }

        return rev;
    }

    // Function to compute x raised to the power y
    static int power(int x, int y)
    {
        // Base case
        if (y == 0)
            return 1;

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

    // Function to return n raised to the power of its
    // reverse
    static int reverseExponentiation(int n)
    {
        int rev = revNum(n);
        return power(n, rev);
    }

    public static void main(String[] args)
    {
        int n = 10;

        System.out.println(reverseExponentiation(n));
    }
}
Python
def revNum(n):
    rev = 0

    while n > 0:
        rev = rev * 10 + n % 10
        n //= 10

    return rev

# Function to compute x raised to the power y


def power(x, y):
    # Base case
    if y == 0:
        return 1

    return x * power(x, y - 1)

# Function to return n raised to the power of its reverse


def reverseExponentiation(n):
    rev = revNum(n)
    return power(n, rev)


if __name__ == '__main__':
    n = 10

    print(reverseExponentiation(n))
C#
using System;

class GFG {
    // Function to reverse the given number
    static int revNum(int n)
    {
        int rev = 0;

        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }

        return rev;
    }

    // Function to compute x raised to the power y
    static int power(int x, int y)
    {
        // Base case
        if (y == 0)
            return 1;

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

    // Function to return n raised to the power of its
    // reverse
    static int reverseExponentiation(int n)
    {
        int rev = revNum(n);
        return power(n, rev);
    }

    static void Main()
    {
        int n = 10;

        Console.WriteLine(reverseExponentiation(n));
    }
}
JavaScript
// Function to reverse the given number
function revNum(n)
{
    let rev = 0;

    while (n > 0) {
        rev = rev * 10 + n % 10;
        n = Math.floor(n / 10);
    }

    return rev;
}

// Function to compute x raised to the power y
function power(x, y)
{
    // Base case
    if (y === 0)
        return 1;

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

// Function to return n raised to the power of its reverse
function reverseExponentiation(n)
{
    let rev = revNum(n);
    return power(n, rev);
}

// Driver Code
let n = 10;
console.log(reverseExponentiation(n));

Output
10

[Expected Approach] Iterative Binary Exponentiation - O(log r) Time and O(1) Space

The idea is to first reverse the given number to obtain the exponent r. Then, compute n^r using Iterative Binary Exponentiation. At each step, if the current exponent is odd, multiply the result by the current base. Square the base and halve the exponent after every iteration until the exponent becomes 0.

Working of Approach:

  • Reverse the given number to obtain the exponent rev.
  • Initialize the result as 1 and the base as n.
  • If the current exponent is odd, multiply the result by the current base.
  • Square the base and divide the exponent by 2 in every iteration.
  • Continue until the exponent becomes 0, then return the computed result.

Let us understand with an example:
Input: n = 10

  • Reverse of 10 is 1, so we need to compute 10 ^ 1.
  • Initialize res = 1, base = 10, and exp = 1.
  • Since exp is odd, update res = 1 × 10 = 10.
  • Square the base (base = 100) and halve the exponent (exp = 0).
  • The exponent becomes 0, so return 10.
C++
#include <iostream>
using namespace std;

// Function to reverse the given number
int revNum(int n)
{
    int rev = 0;

    while (n > 0)
    {
        rev = rev * 10 + n % 10;
        n /= 10;
    }

    return rev;
}

// Function to compute x raised to the power y
int power(int x, int y)
{
    int res = 1;

    while (y > 0)
    {
        // If exponent is odd
        if (y & 1)
            res *= x;

        // Square the base
        x *= x;

        // Divide exponent by 2
        y >>= 1;
    }

    return res;
}

// Function to return n raised to the power of its reverse
int reverseExponentiation(int n)
{
    int rev = revNum(n);
    return power(n, rev);
}

int main()
{
    int n = 10;

    cout << reverseExponentiation(n);

    return 0;
}
Java
public class GFG {
    // Function to reverse the given number
    public static int revNum(int n)
    {
        int rev = 0;
        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }
        return rev;
    }

    // Function to compute x raised to the power y
    public static int power(int x, int y)
    {
        int res = 1;
        while (y > 0) {
            // If exponent is odd
            if ((y & 1) == 1)
                res *= x;
            // Square the base
            x *= x;
            // Divide exponent by 2
            y >>= 1;
        }
        return res;
    }

    // Function to return n raised to the power of its
    // reverse
    public static int reverseExponentiation(int n)
    {
        int rev = revNum(n);
        return power(n, rev);
    }

    public static void main(String[] args)
    {
        int n = 10;
        System.out.println(reverseExponentiation(n));
    }
}
Python
def revNum(n):
    rev = 0
    while n > 0:
        rev = rev * 10 + n % 10
        n //= 10
    return rev

# Function to compute x raised to the power y


def power(x, y):
    res = 1
    while y > 0:
        # If exponent is odd
        if y & 1:
            res *= x
        # Square the base
        x *= x
        # Divide exponent by 2
        y >>= 1
    return res

# Function to return n raised to the power of its reverse


def reverseExponentiation(n):
    rev = revNum(n)
    return power(n, rev)


if __name__ == '__main__':
    n = 10

    print(reverseExponentiation(n))
C#
using System;

public class GFG {
    // Function to reverse the given number
    public static int revNum(int n)
    {
        int rev = 0;
        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }
        return rev;
    }

    // Function to compute x raised to the power y
    public static int power(int x, int y)
    {
        int res = 1;
        while (y > 0) {
            // If exponent is odd
            if ((y & 1) == 1)
                res *= x;
            // Square the base
            x *= x;
            // Divide exponent by 2
            y >>= 1;
        }
        return res;
    }

    // Function to return n raised to the power of its
    // reverse
    public static int reverseExponentiation(int n)
    {
        int rev = revNum(n);
        return power(n, rev);
    }

    public static void Main()
    {
        int n = 10;
        Console.WriteLine(reverseExponentiation(n));
    }
}
JavaScript
// Function to reverse the given number
function revNum(n)
{
    let rev = 0;
    while (n > 0) {
        rev = rev * 10 + n % 10;
        n = Math.floor(n / 10);
    }
    return rev;
}

// Function to compute x raised to the power y
function power(x, y)
{
    let res = 1;
    while (y > 0) {
        // If exponent is odd
        if (y & 1)
            res *= x;
        // Square the base
        x *= x;
        // Divide exponent by 2
        y = Math.floor(y / 2);
    }
    return res;
}

// Function to return n raised to the power of its reverse
function reverseExponentiation(n)
{
    let rev = revNum(n);
    return power(n, rev);
}

// Driver Code
let n = 10;
console.log(reverseExponentiation(n));

Output
10
Comment