-2

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);
        }
  • IIRC my binary properly, -3 equals 1/2^3. In other words, 1/8 or 0.125. Unfortunatley that means you *have* to return a floating point value. And those have issues with precision and determinism: https://www.youtube.com/watch?v=PZRI1IfStY0 – Christopher Nov 15 '19 at 23:09
  • So you can pretty much get the power for the Absolute value as usual. Just if the input was negative, the return value is used to divide 1 instead of instantly returned. Of course with a negative base, stuff becomes tricky. – Christopher Nov 15 '19 at 23:11
  • 1
    It's not clear what you're asking. You can't return fractional values if your method returns `int`. Are you asking how to change the return type of your method? Are you asking how to actually _compute_ the fractional value? That should just be a matter of `return 1 / power(x, -n)`, assuming you've changed the return type to `double` or `float`. Please improve your question. – Peter Duniho Nov 15 '19 at 23:21
  • I'll also point out that the `n == 1` case unnecessarily complicates the code. You could just leave that out. It's not like efficiency is a priority anyway (which I take as a given, since you're reinventing the wheel here :) ) – Peter Duniho Nov 15 '19 at 23:22
  • see [Power by squaring for negative exponents](https://stackoverflow.com/a/30962495/2521214) it handles more cases than just negative exponents ... – Spektre Nov 17 '19 at 09:04

1 Answers1

1

Since the result of raising a number to a negative power is just 1 divided by the number raised to the non-negative power, you can change you method like so (note that we also need to return a double type, since we're dealing with fractional values):

public static double power(int x, int n)
{
    if (n < 0) return 1 / power(x, -n);  // recursive call with '-n' for negative powers
    if (n == 0) return 1;
    if (n == 1) return x;
    return x * power(x, n - 1);
}

Now it works with negative numbers:

public static void Main(string[] args)
{
    Console.WriteLine(power(2, -3));
    GetKeyFromUser("\nDone! Press any key to exit...");
}

Output

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43