Calculate power of a number using recursion

Here given code implementation process.

// 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


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







© 2021, kalkicode.com, All rights reserved