asked 84.9k views
0 votes
Write the definition of a method powerto, which receives two parameters. the first is a double and the second is an int. the method returns a double. if the second parameter is negative, the method returns zero. otherwise it returns the value of the first parameter raised to the power of the second parameter.

1 Answer

5 votes

The case of x⁰=1 is one you shouldn't overlook!

double powerto(double f, int exponent)
{
if (exponent < 0) { return 0; }
if (exponent == 0) { return 1.0; }
while (--exponent > 0) { f *= f; }
return f;
}