Program to Calculate the Power using Recursion
- Write a program to Calculate the Power using Recursion in C
- Write a program to Calculate the Power using Recursion in C++
- Write a program to Calculate the Power using Recursion in Python
- Write a program to Calculate the Power using Recursion in PHP
- Write a program to Calculate the Power using Recursion in Java
- Write a program to Calculate the Power using Recursion in Java Script
- Write a program to Calculate the Power using Recursion in C#
Explanation:
Logic to Calculate Power Using Recursion
To calculate the power of a number using recursion, the general idea is to break the problem down into smaller subproblems using the following properties of exponents:
- Base Case:
- If the exponent is 0, the result is always 1 (since any number raised to the power of 0 is 1).
- Recursive Case:
- If the exponent is greater than 0, we can reduce the exponent by 1 and multiply the base with the result of raising the base to the power of the reduced exponent.
Program to Calculate the Power using Recursion
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int power(int base, int exp) { if (exp == 0) return 1; // Base case: any number raised to 0 is 1 return base * power(base, exp - 1); // Recursive case } int main() { int base, exp; printf("Enter base: "); scanf("%d", &base); printf("Enter exponent: "); scanf("%d", &exp); printf("%d raised to the power of %d is %d\n", base, exp, power(base, exp)); return 0; }
#include <iostream> using namespace std; int power(int base, int exp) { if (exp == 0) return 1; // Base case: any number raised to 0 is 1 return base * power(base, exp - 1); // Recursive case } int main() { int base, exp; cout << "Enter base: "; cin >> base; cout << "Enter exponent: "; cin >> exp; cout << base << " raised to the power of " << exp << " is " << power(base, exp) << endl; return 0; }
def power(base, exp): if exp == 0: return 1 # Base case: any number raised to 0 is 1 return base * power(base, exp - 1) # Recursive case base = int(input("Enter base: ")) exp = int(input("Enter exponent: ")) print(f"{base} raised to the power of {exp} is {power(base, exp)}")
<?php function power($base, $exp) { if ($exp == 0) return 1; // Base case: any number raised to 0 is 1 return $base * power($base, $exp - 1); // Recursive case } echo "Enter base: "; $base = intval(trim(fgets(STDIN))); echo "Enter exponent: "; $exp = intval(trim(fgets(STDIN))); echo "$base raised to the power of $exp is " . power($base, $exp) . "\n"; ?>
import java.util.Scanner; public class Main { public static int power(int base, int exp) { if (exp == 0) return 1; // Base case: any number raised to 0 is 1 return base * power(base, exp - 1); // Recursive case } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter base: "); int base = scanner.nextInt(); System.out.print("Enter exponent: "); int exp = scanner.nextInt(); System.out.println(base + " raised to the power of " + exp + " is " + power(base, exp)); } }
function power(base, exp) { if (exp === 0) return 1; // Base case: any number raised to 0 is 1 return base * power(base, exp - 1); // Recursive case } const base = parseInt(prompt("Enter base:")); const exp = parseInt(prompt("Enter exponent:")); console.log(`${base} raised to the power of ${exp} is ${power(base, exp)}`);
using System; class Program { static int Power(int baseVal, int exp) { if (exp == 0) return 1; // Base case: any number raised to 0 is 1 return baseVal * Power(baseVal, exp - 1); // Recursive case } static void Main() { Console.Write("Enter base: "); int baseVal = int.Parse(Console.ReadLine()); Console.Write("Enter exponent: "); int exp = int.Parse(Console.ReadLine()); Console.WriteLine($"{baseVal} raised to the power of {exp} is {Power(baseVal, exp)}"); } }