I'm trying to make a function that can calculate a number raised to a power, but my console isn't loading so I can't see what it's doing. Can anyone check it for me? Also, I need to test all the possible outcomes, my code doesn't test decimals, any suggestions?
Thanks
zero raised to any power should be zero. Also, your for loop conditions need to be different for negative numbers. It's probably best to just rewrite your code to use the absolute value of the exponent, that way you only need to write one loop.
Actually, 00 is an indeterminate form, but in many cases, it's treated as 1 just for simplicity/laziness/consistency.
Also, zero raised to a negative power is undefined (division by zero).
As far as your actual program goes, you just need to change this for loop:
29 30 31 32
for (int num = exp; num > 1; num--)
{
product *= base;
}
Since exp is less than 0, this should be for (int num = -exp; num > 1; num--)
instead.