- Course
- Recursion
- Calculate a power recursively
Calculate a power recursively
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that implements a recursive function that calculates the result of raising an integer to another integer. This function must be created recursively.
Input
2
3
Output
8
Solution
using System;
public class CalculatePowerRecursively
{
public static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int exponent = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Power(number, exponent));
}
public static int Power(int number, int exponent)
{
if (exponent == 0)
{
return 1;
}
else
{
return number * Power(number, exponent - 1);
}
}
}