I need to write a recursive method Power(base, exponent)
that, when called, returns Power(3,4) = 3*3*3*3
. And exponent
is an integer greater than or equal to 1. Below is my code, but it is wrong. I want to use factorial, but i know little about factorial.
public static void Main(string[] args)
{
Console.WriteLine("please enter base,\n" + "please enter exponent,\n" + "pressing 'Enter'after each one");
double number1 = Convert.ToDouble(Console.ReadLine());
int number2 = Convert.ToInt16(Console.ReadLine());
double result = Pow(number1, number2);
Console.WriteLine("pow({0},{1}) is {2}",number1,number2,result);
}
public static long Factorial(long number)
{
if (number <= 1)
return 1;
else
return number * Factorial(number - 1);
}
public static double Pow(double basevalue, int exponentvalue)
{
double a=1;
if (exponentvalue==1)
{
Console.WriteLine("It is not make sense!");
return basevalue;
}
else if(exponentvalue > 1)
{
a=exponentvalue * Factorial(exponentvalue - 1);
}
return a;
}