Posted on by Kalkicode
Code Recursion

# Calculate power of a number using recursion

The power of a number is a mathematical concept that represents the result of raising a given base number to a specified exponent. It is commonly denoted as "base^exponent" or "base^power." The base number is multiplied by itself a certain number of times, as indicated by the exponent, to yield the final result, which is known as the power.

Calculating the power of a number using recursion is a common mathematical problem where we need to raise a given number to a specified power. Recursion is a programming technique in which a function calls itself to solve smaller subproblems. In this case, we'll create a recursive function to compute the power of a number efficiently.

## Problem Statement

Given an integer 'number' and an integer 'power,' the task is to calculate 'number' raised to the power 'power' using recursion.

## Example

Let's consider the number=2 and power=3. To calculate 2^3, we can break it down into smaller subproblems. 2^3 can be expressed as 2 * 2^2. Similarly, 2^2 can be further expressed as 2 * 2^1, and 2^1 is just the number 2 itself.

## Standard Pseudocode

``````pow_number(number, power):
if power is 0:
return 1
else if power is negative:
return 1 / pow_number(number, -power)
else:
return number * pow_number(number, power - 1)
``````

## Algorithm Explanation

1. The function `pow_number` takes two parameters: 'number' and 'power.'
2. If the 'power' is 0, it means any number raised to the power of 0 is 1, so we return 1.
3. If the 'power' is negative, we handle it by returning the reciprocal of the result of the recursive call with the absolute value of 'power.'
4. If the 'power' is positive, we recursively call the function with the 'power' decremented by 1 and multiply it with the 'number' to get the final result.

## Program List

``````// C program
// Calculate power of a number using recursion
#include <stdio.h>

// Calculate the power of a number
double pow_number(int number, int power)
{
if (power == 0)
{
return 1;
}
else if (power < 0)
{
//When get negative power
return (1 / pow_number(number, -power));
}
else
{
return number * pow_number(number, power - 1);
}
}
//
void find_power(int number, int power)
{
//Display result
printf("\n Given  : (%d^(%d)) ", number, power);
printf("\n Result : %lf \n", pow_number(number, power));
}
int main()
{
//Test Cases
find_power(2, -2);
find_power(2, 2);
find_power(7, -5);
find_power(4, 5);
return 0;
}``````

#### Output

`````` Given  : (2^(-2))
Result : 0.250000

Given  : (2^(2))
Result : 4.000000

Given  : (7^(-5))
Result : 0.000059

Given  : (4^(5))
Result : 1024.000000``````
``````// Java program
// Calculate power of a number using recursion
class PowerCalculation
{
// Calculate the power of a number
public double pow_number(int number, int power)
{
if (power == 0)
{
return 1;
}
else if (power < 0)
{
//When get negative power
return (1 / pow_number(number, -power));
}
else
{
return number * pow_number(number, power - 1);
}
}
//
public void find_power(int number, int power)
{
//Display result
System.out.print("\n Given : (" + number + "^(" + power + ")) ");
System.out.print("\n Result : " + pow_number(number, power) + " \n");
}
public static void main(String[] args)
{
PowerCalculation obj = new PowerCalculation();
//Test Cases
obj.find_power(2, -2);
obj.find_power(2, 2);
obj.find_power(7, -5);
obj.find_power(4, 5);
}
}``````

#### Output

`````` Given : (2^(-2))
Result : 0.25

Given : (2^(2))
Result : 4.0

Given : (7^(-5))
Result : 5.9499018266198606E-5

Given : (4^(5))
Result : 1024.0``````
``````//Include header file
#include <iostream>
using namespace std;

// C++ program
// Calculate power of a number using recursion

class PowerCalculation
{
public:
// Calculate the power of a number
double pow_number(int number, int power)
{
if (power == 0)
{
return 1;
}
else if (power < 0)
{
//When get negative power
return (1 / this->pow_number(number, -power));
}
else
{
return number * this->pow_number(number, power - 1);
}
}
//
void find_power(int number, int power)
{
//Display result
cout << "\n Given : (" << number << "^(" << power << ")) ";
cout << "\n Result : " << this->pow_number(number, power) << " \n";
}
};
int main()
{
PowerCalculation obj = PowerCalculation();
//Test Cases
obj.find_power(2, -2);
obj.find_power(2, 2);
obj.find_power(7, -5);
obj.find_power(4, 5);
return 0;
}``````

#### Output

`````` Given : (2^(-2))
Result : 0.25

Given : (2^(2))
Result : 4

Given : (7^(-5))
Result : 5.9499e-05

Given : (4^(5))
Result : 1024``````
``````<?php
// Php program
// Calculate power of a number using recursion
class PowerCalculation
{
// Calculate the power of a number
public	function pow_number(\$number, \$power)
{
if (\$power == 0)
{
return 1;
}
else if (\$power < 0)
{
//When get negative power
return (1 / \$this->pow_number(\$number, -\$power));
}
else
{
return \$number * \$this->pow_number(\$number, \$power - 1);
}
}
//
public	function find_power(\$number, \$power)
{
//Display result
echo "\n Given : (". \$number ."^(". \$power .")) ";
echo "\n Result : ". \$this->pow_number(\$number, \$power) ." \n";
}
}

function main()
{
\$obj = new PowerCalculation();
//Test Cases
\$obj->find_power(2, -2);
\$obj->find_power(2, 2);
\$obj->find_power(7, -5);
\$obj->find_power(4, 5);
}
main();``````

#### Output

`````` Given : (2^(-2))
Result : 0.25

Given : (2^(2))
Result : 4

Given : (7^(-5))
Result : 5.9499018266199E-5

Given : (4^(5))
Result : 1024``````
``````//Include namespace system
using System;
// C# program
// Calculate power of a number using recursion
class PowerCalculation
{
// Calculate the power of a number
public double pow_number(int number, int power)
{
if (power == 0)
{
return 1;
}
else if (power < 0)
{
//When get negative power
return (1 / pow_number(number, -power));
}
else
{
return number * pow_number(number, power - 1);
}
}
//
public void find_power(int number, int power)
{
//Display result
Console.Write("\n Given : (" + number + "^(" + power + ")) ");
Console.Write("\n Result : " + pow_number(number, power) + " \n");
}
public static void Main(String[] args)
{
PowerCalculation obj = new PowerCalculation();
//Test Cases
obj.find_power(2, -2);
obj.find_power(2, 2);
obj.find_power(7, -5);
obj.find_power(4, 5);
}
}``````

#### Output

`````` Given : (2^(-2))
Result : 0.25

Given : (2^(2))
Result : 4

Given : (7^(-5))
Result : 5.94990182661986E-05

Given : (4^(5))
Result : 1024``````
``````// Node Js program
// Calculate power of a number using recursion
class PowerCalculation
{
// Calculate the power of a number
pow_number(number, power)
{
if (power == 0)
{
return 1;
}
else if (power < 0)
{
//When get negative power
return (1 / this.pow_number(number, -power));
}
else
{
return number * this.pow_number(number, power - 1);
}
}
//
find_power(number, power)
{
//Display result
process.stdout.write("\n Given : (" + number + "^(" + power + ")) ");
process.stdout.write("\n Result : " + this.pow_number(number, power) + " \n");
}
}

function main()
{
var obj = new PowerCalculation();
//Test Cases
obj.find_power(2, -2);
obj.find_power(2, 2);
obj.find_power(7, -5);
obj.find_power(4, 5);
}
main();``````

#### Output

`````` Given : (2^(-2))
Result : 0.25

Given : (2^(2))
Result : 4

Given : (7^(-5))
Result : 0.000059499018266198606

Given : (4^(5))
Result : 1024``````
``````#  Python 3 program
#  Calculate power of a number using recursion
class PowerCalculation :
#  Calculate the power of a number
def pow_number(self, number, power) :
if (power == 0) :
return 1

elif(power < 0) :
# When get negative power
return ((1 / self.pow_number(number, -power)))
else :
return number * self.pow_number(number, power - 1)

#
def find_power(self, number, power) :
# Display result
print("\n Given : (", number ,"^(", power ,")) ", end = "")
print("\n Result : ", self.pow_number(number, power) ," \n", end = "")

def main() :
obj = PowerCalculation()
# Test Cases
obj.find_power(2, -2)
obj.find_power(2, 2)
obj.find_power(7, -5)
obj.find_power(4, 5)

if __name__ == "__main__": main()``````

#### Output

`````` Given : ( 2 ^( -2 ))
Result :  0.25

Given : ( 2 ^( 2 ))
Result :  4

Given : ( 7 ^( -5 ))
Result :  5.9499018266198606e-05

Given : ( 4 ^( 5 ))
Result :  1024``````
``````#  Ruby program
#  Calculate power of a number using recursion
class PowerCalculation

#  Calculate the power of a number
def pow_number(number, power)

if (power == 0)

return 1
elsif(power < 0)

# When get negative power
return (1 / self.pow_number(number, -power).to_f)
else

return number * self.pow_number(number, power - 1)
end
end
#
def find_power(number, power)

# Display result
print("\n Given : (", number ,"^(", power ,")) ")
print("\n Result : ", self.pow_number(number, power) ," \n")
end
end
def main()

obj = PowerCalculation.new()
# Test Cases
obj.find_power(2, -2)
obj.find_power(2, 2)
obj.find_power(7, -5)
obj.find_power(4, 5)
end
main()``````

#### Output

`````` Given : (2^(-2))
Result : 0.25

Given : (2^(2))
Result : 4

Given : (7^(-5))
Result : 5.9499018266198606e-05

Given : (4^(5))
Result : 1024
``````
``````// Scala program
// Calculate power of a number using recursion
class PowerCalculation
{
// Calculate the power of a number
def pow_number(number: Int, power: Int): Double = {
if (power == 0)
{
return 1;
}
else if (power < 0)
{
//When get negative power
return (1 / pow_number(number, -power));
}
else
{
return number * pow_number(number, power - 1);
}
}
//
def find_power(number: Int, power: Int): Unit = {
//Display result
print("\n Given : (" + number + "^(" + power + ")) ");
print("\n Result : " + pow_number(number, power) + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: PowerCalculation = new PowerCalculation();
//Test Cases
obj.find_power(2, -2);
obj.find_power(2, 2);
obj.find_power(7, -5);
obj.find_power(4, 5);
}
}``````

#### Output

`````` Given : (2^(-2))
Result : 0.25

Given : (2^(2))
Result : 4.0

Given : (7^(-5))
Result : 5.9499018266198606E-5

Given : (4^(5))
Result : 1024.0``````
``````// Swift program
// Calculate power of a number using recursion
class PowerCalculation
{
// Calculate the power of a number
func pow_number(_ number: Int, _ power: Int) -> Double
{
if (power == 0)
{
return 1;
}
else if (power < 0)
{
//When get negative power
return (Double(1) / self.pow_number(number, -power));
}
else
{
return Double(number) * self.pow_number(number, power - 1);
}
}
//
func find_power(_ number: Int, _ power: Int)
{
//Display result
print("\n Given : (", number ,"^(", power ,")) ", terminator: "");
print("\n Result : ", self.pow_number(number, power) ," \n", terminator: "");
}
}
func main()
{
let obj: PowerCalculation = PowerCalculation();
//Test Cases
obj.find_power(2, -2);
obj.find_power(2, 2);
obj.find_power(7, -5);
obj.find_power(4, 5);
}
main();``````

#### Output

`````` Given : ( 2 ^( -2 ))
Result :  0.25

Given : ( 2 ^( 2 ))
Result :  4.0

Given : ( 7 ^( -5 ))
Result :  5.94990182661986e-05

Given : ( 4 ^( 5 ))
Result :  1024.0``````

## Resultant Output Explanation

1. find_power(2, -2): Given: (2^(-2)) Result: 0.25 Explanation: 2^(-2) = 1 / (2^2) = 1 / 4 = 0.25

2. find_power(2, 2): Given: (2^(2)) Result: 4.0 Explanation: 2^(2) = 2 * (2^1) = 2 * 2 = 4

3. find_power(7, -5): Given: (7^(-5)) Result: 5.89e-05 (approximately 0.000059) Explanation: 7^(-5) = 1 / (7^5) = 1 / 16807 ≈ 5.89e-05

4. find_power(4, 5): Given: (4^(5)) Result: 1024.0 Explanation: 4^(5) = 4 * (4^4) = 4 * 256 = 1024

## Time Complexity

The time complexity of the recursive function is O(power). This is because the function makes 'power' recursive calls, and each call takes constant time (O(1)) to compute the result. However, keep in mind that recursive solutions may have higher overhead due to function call stack space usage. In practice, it's recommended to use

## 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.

Categories
Relative Post