I made this program to find the power of any number using recursion and it works, but also I need to find negative power of the number, for example, I have the base = 2 and the exponent = -3 so the result = 0.125, what should I do?
public static int power(int x, int n )
{
if (n < 0)
{
Console.WriteLine("invalid");
return 0;
}
else if (n == 1)
{
return x;
}
else if (n == 0)
{
return 1;
}
else
{
return x * power(x, n - 1);
}
}
static void Main(string[] args)
{
Console.Write("enter the base: ");
int x = int.Parse(Console.ReadLine());
Console.Write("enter the power:");
int n = int.Parse(Console.ReadLine());
int z = power(x, n);
Console.WriteLine(z);
}