Skip to main content

Calculate power of two numbers

Calculating the power of two numbers involves multiplying a base number by itself a certain number of times.

For example, if you want to find 3 raised to the power of 4, you would multiply 3 by itself 4 times, like this:

3 x 3 x 3 x 3 = 81

In this case, 3 is the base number, and 4 is the exponent or power.

A general formula for calculating the power of a number is:

base ^ exponent

where "^" represents "to the power of".

So, to calculate the power of any two numbers, you can use this formula and substitute the values of the base and exponent as needed.

For example, to calculate 5 raised to the power of 3, you would write:

5^3 = 5 x 5 x 5 = 125

Similarly, to calculate 2 raised to the power of 6, you would write:

2^6 = 2 x 2 x 2 x 2 x 2 x 2 = 64

So, the power of two numbers can be calculated using this simple formula.

Here given code implementation process.

//  C program for
//  Calculate power of two numbers
#include <stdio.h>

int findPower(int x, unsigned int p)
{
    if (p == 0)
    {
        // When p is zero
        return 1;
    }
    if ((p % 2) == 0)
    {
        // p is Even number
        return findPower(x, p / 2) * findPower(x, p / 2);
    }
    else
    {
        // p is Odd number
        return x * findPower(x, p / 2) * findPower(x, p / 2);
    }
}
int main(int argc, char const * argv[])
{
    // Test
    printf("\n %d", findPower(3, 3));
    printf("\n %d", findPower(4, 5));
    return 0;
}

Output

 27
 1024
// Java program
// Calculate power of two numbers
public class Power
{
    public int findPower(int x, int p)
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        if ((p % 2) == 0)
        {
            // p is Even number
            return findPower(x, p / 2) * findPower(x, p / 2);
        }
        else
        {
            // p is Odd number
            return x * findPower(x, p / 2) * findPower(x, p / 2);
        }
    }
    public static void main(String[] args)
    {
        Power task = new Power();
        // Test
        System.out.print("\n " + task.findPower(3, 3));
        System.out.print("\n " + task.findPower(4, 5));
    }
}

Output

 27
 1024
// Include header file
#include <iostream>
using namespace std;
// C++ program
// Calculate power of two numbers
class Power
{
    public: int findPower(int x, int p)
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        if ((p % 2) == 0)
        {
            // p is Even number
            return this->findPower(x, p / 2) * this->findPower(x, p / 2);
        }
        else
        {
            // p is Odd number
            return x * this->findPower(x, p / 2) * this->findPower(x, p / 2);
        }
    }
};
int main()
{
    Power *task = new Power();
    // Test
    cout << "\n " << task->findPower(3, 3);
    cout << "\n " << task->findPower(4, 5);
    return 0;
}

Output

 27
 1024
package main
import "fmt"
// Go program
// Calculate power of two numbers
type Power struct {}
func getPower() * Power {
    var me *Power = &Power {}
    return me
}
func(this Power) findPower(x, p int) int {
    if p == 0 {
        // When p is zero
        return 1
    }
    if (p % 2) == 0 {
        // p is Even number
        return this.findPower(x, p / 2) * this.findPower(x, p / 2)
    } else {
        // p is Odd number
        return x * this.findPower(x, p / 2) * this.findPower(x, p / 2)
    }
}
func main() {
    var task * Power = getPower()
    // Test
    fmt.Print("\n ", task.findPower(3, 3))
    fmt.Print("\n ", task.findPower(4, 5))
}

Output

 27
 1024
// Include namespace system
using System;
// Csharp program
// Calculate power of two numbers
public class Power
{
    public int findPower(int x, int p)
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        if ((p % 2) == 0)
        {
            // p is Even number
            return this.findPower(x, p / 2) * this.findPower(x, p / 2);
        }
        else
        {
            // p is Odd number
            return x * this.findPower(x, p / 2) * this.findPower(x, p / 2);
        }
    }
    public static void Main(String[] args)
    {
        Power task = new Power();
        // Test
        Console.Write("\n " + task.findPower(3, 3));
        Console.Write("\n " + task.findPower(4, 5));
    }
}

Output

 27
 1024
<?php
// Php program
// Calculate power of two numbers
class Power
{
    public  function findPower($x, $p)
    {
        if ($p == 0)
        {
            // When p is zero
            return 1;
        }
        if (($p % 2) == 0)
        {
            // p is Even number
            return $this->findPower($x, (int)($p / 2)) * 
              $this->findPower($x, (int)($p / 2));
        }
        else
        {
            // p is Odd number
            return $x * $this->findPower($x, (int)($p / 2)) * 
              $this->findPower($x, (int)($p / 2));
        }
    }
}

function main()
{
    $task = new Power();
    // Test
    echo("\n ".$task->findPower(3, 3));
    echo("\n ".$task->findPower(4, 5));
}
main();

Output

 27
 1024
// Node JS program
// Calculate power of two numbers
class Power
{
    findPower(x, p)
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        if ((p % 2) == 0)
        {
            // p is Even number
            return this.findPower(x, parseInt(p / 2)) * 
              this.findPower(x, parseInt(p / 2));
        }
        else
        {
            // p is Odd number
            return x * this.findPower(x, parseInt(p / 2)) * 
              this.findPower(x, parseInt(p / 2));
        }
    }
}

function main()
{
    var task = new Power();
    // Test
    process.stdout.write("\n " + task.findPower(3, 3));
    process.stdout.write("\n " + task.findPower(4, 5));
}
main();

Output

 27
 1024
#  Python 3 program
#  Calculate power of two numbers
class Power :
    def findPower(self, x, p) :
        if (p == 0) :
            #  When p is zero
            return 1
        
        if ((p % 2) == 0) :
            #  p is Even number
            return self.findPower(x, 
                                  int(p / 2)) * self.findPower(x, 
                                                               int(p / 2))
        else :
            #  p is Odd number
            return x * self.findPower(x, 
                                      int(p / 2)) * self.findPower(x, 
                                                                   int(p / 2))
        
    

def main() :
    task = Power()
    #  Test
    print("\n ", task.findPower(3, 3), end = "")
    print("\n ", task.findPower(4, 5), end = "")

if __name__ == "__main__": main()

Output

  27
  1024
#  Ruby program
#  Calculate power of two numbers
class Power 
    def findPower(x, p) 
        if (p == 0) 
            #  When p is zero
            return 1
        end

        if ((p % 2) == 0) 
            #  p is Even number
            return self.findPower(x, p / 2) * 
              self.findPower(x, p / 2)
        else
 
            #  p is Odd number
            return x * self.findPower(x, p / 2) * 
              self.findPower(x, p / 2)
        end

    end

end

def main() 
    task = Power.new()
    #  Test
    print("\n ", task.findPower(3, 3))
    print("\n ", task.findPower(4, 5))
end

main()

Output

 27
 1024
// Scala program
// Calculate power of two numbers
class Power()
{
    def findPower(x: Int, p: Int): Int = {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        if ((p % 2) == 0)
        {
            // p is Even number
            return findPower(x, p / 2) * findPower(x, p / 2);
        }
        else
        {
            // p is Odd number
            return x * findPower(x, p / 2) * findPower(x, p / 2);
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Power = new Power();
        // Test
        print("\n " + task.findPower(3, 3));
        print("\n " + task.findPower(4, 5));
    }
}

Output

 27
 1024
// Kotlin program
// Calculate power of two numbers
class Power
{
    fun findPower(x: Int, p: Int): Int
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        if ((p % 2) == 0)
        {
            // p is Even number
            return this.findPower(x, p / 2) * this.findPower(x, p / 2);
        }
        else
        {
            // p is Odd number
            return x * this.findPower(x, p / 2) * this.findPower(x, p / 2);
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Power = Power();
    // Test
    print("\n " + task.findPower(3, 3));
    print("\n " + task.findPower(4, 5));
}

Output

 27
 1024

Efficient Solution

//  C program for
//  Calculate power of two numbers efficiently
#include <stdio.h>

int findPower(int x, unsigned int p)
{
    if (p == 0)
    {
        // When p is zero
        return 1;
    }
    int pow = findPower(x, p / 2);
    if ((p % 2) == 0)
    {
        // p is Even number
        return pow *pow;
    }
    else
    {
        // p is Odd number
        return x *pow *pow;
    }
}
int main(int argc, char
    const *argv[])
{
    // Test
    printf("\n %d", findPower(3, 3));
    printf("\n %d", findPower(4, 5));
    return 0;
}

Output

 27
 1024
// Java program
// Calculate power of two numbers efficiently

public class Power
{
    public int findPower(int x, int p)
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }

        int pow = findPower(x, p / 2);
        
        if ((p % 2) == 0)
        {
            // p is Even number
            return pow * pow;
        }
        else
        {
            // p is Odd number
            return x * pow * pow;
        }
    }
    public static void main(String[] args)
    {
        Power task = new Power();

        // Test
        System.out.print("\n " + task.findPower(3, 3));
        System.out.print("\n " + task.findPower(4, 5));
    }
}

Output

 27
 1024
// Include header file
#include <iostream>
using namespace std;
// C++ program
// Calculate power of two numbers efficiently
class Power
{
    public: int findPower(int x, int p)
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        int pow = this->findPower(x, p / 2);
        if ((p % 2) == 0)
        {
            // p is Even number
            return pow *pow;
        }
        else
        {
            // p is Odd number
            return x *pow *pow;
        }
    }
};
int main()
{
    Power *task = new Power();
    // Test
    cout << "\n " << task->findPower(3, 3);
    cout << "\n " << task->findPower(4, 5);
    return 0;
}

Output

 27
 1024
package main
import "fmt"
// Go program
// Calculate power of two numbers efficiently
type Power struct {}
func getPower() * Power {
    var me *Power = &Power {}
    return me
}
func(this Power) findPower(x, p int) int {
    if p == 0 {
        // When p is zero
        return 1
    }
    var pow int = this.findPower(x, p / 2)
    if (p % 2) == 0 {
        // p is Even number
        return pow * pow
    } else {
        // p is Odd number
        return x * pow * pow
    }
}
func main() {
    var task * Power = getPower()
    // Test
    fmt.Print("\n ", task.findPower(3, 3))
    fmt.Print("\n ", task.findPower(4, 5))
}

Output

 27
 1024
// Include namespace system
using System;
// Csharp program
// Calculate power of two numbers efficiently
public class Power
{
    public int findPower(int x, int p)
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        int pow = this.findPower(x, p / 2);
        if ((p % 2) == 0)
        {
            // p is Even number
            return pow * pow;
        }
        else
        {
            // p is Odd number
            return x * pow * pow;
        }
    }
    public static void Main(String[] args)
    {
        Power task = new Power();
        // Test
        Console.Write("\n " + task.findPower(3, 3));
        Console.Write("\n " + task.findPower(4, 5));
    }
}

Output

 27
 1024
<?php
// Php program
// Calculate power of two numbers efficiently
class Power
{
    public  function findPower($x, $p)
    {
        if ($p == 0)
        {
            // When p is zero
            return 1;
        }
        $pow = $this->findPower($x, (int)($p / 2));
        if (($p % 2) == 0)
        {
            // p is Even number
            return $pow * $pow;
        }
        else
        {
            // p is Odd number
            return $x * $pow * $pow;
        }
    }
}

function main()
{
    $task = new Power();
    // Test
    echo("\n ".$task->findPower(3, 3));
    echo("\n ".$task->findPower(4, 5));
}
main();

Output

 27
 1024
// Node JS program
// Calculate power of two numbers efficiently
class Power
{
    findPower(x, p)
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        var pow = this.findPower(x, parseInt(p / 2));
        if ((p % 2) == 0)
        {
            // p is Even number
            return pow * pow;
        }
        else
        {
            // p is Odd number
            return x * pow * pow;
        }
    }
}

function main()
{
    var task = new Power();
    // Test
    process.stdout.write("\n " + task.findPower(3, 3));
    process.stdout.write("\n " + task.findPower(4, 5));
}
main();

Output

 27
 1024
#  Python 3 program
#  Calculate power of two numbers efficiently
class Power :
    def findPower(self, x, p) :
        if (p == 0) :
            #  When p is zero
            return 1
        
        pow = self.findPower(x, int(p / 2))
        if ((p % 2) == 0) :
            #  p is Even number
            return pow * pow
        else :
            #  p is Odd number
            return x * pow * pow
        
    

def main() :
    task = Power()
    #  Test
    print(task.findPower(3, 3))
    print(task.findPower(4, 5))

if __name__ == "__main__": main()

Output

27
1024
#  Ruby program
#  Calculate power of two numbers efficiently
class Power 
    def findPower(x, p) 
        if (p == 0) 
            #  When p is zero
            return 1
        end

        pow = self.findPower(x, p / 2)
        if ((p % 2) == 0) 
            #  p is Even number
            return pow * pow
        else
 
            #  p is Odd number
            return x * pow * pow
        end

    end

end

def main() 
    task = Power.new()
    #  Test
    print("\n ", task.findPower(3, 3))
    print("\n ", task.findPower(4, 5))
end

main()

Output

 27
 1024
// Scala program
// Calculate power of two numbers efficiently
class Power()
{
    def findPower(x: Int, p: Int): Int = {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        var pow: Int = findPower(x, p / 2);
        if ((p % 2) == 0)
        {
            // p is Even number
            return pow * pow;
        }
        else
        {
            // p is Odd number
            return x * pow * pow;
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Power = new Power();
        // Test
        print("\n " + task.findPower(3, 3));
        print("\n " + task.findPower(4, 5));
    }
}

Output

 27
 1024
// Swift 4 program
// Calculate power of two numbers efficiently
class Power
{
    func findPower(_ x: Int, _ p: Int) -> Int
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        let pow: Int = self.findPower(x, p / 2);
        if ((p % 2) == 0)
        {
            // p is Even number
            return pow * pow;
        }
        else
        {
            // p is Odd number
            return x * pow * pow;
        }
    }
}
func main()
{
    let task: Power = Power();
    // Test
    print("\n ", task.findPower(3, 3), terminator: "");
    print("\n ", task.findPower(4, 5), terminator: "");
}
main();

Output

  27
  1024
// Kotlin program
// Calculate power of two numbers efficiently
class Power
{
    fun findPower(x: Int, p: Int): Int
    {
        if (p == 0)
        {
            // When p is zero
            return 1;
        }
        val pow: Int = this.findPower(x, p / 2);
        if ((p % 2) == 0)
        {
            // p is Even number
            return pow * pow;
        }
        else
        {
            // p is Odd number
            return x * pow * pow;
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Power = Power();
    // Test
    print("\n " + task.findPower(3, 3));
    print("\n " + task.findPower(4, 5));
}

Output

 27
 1024




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment