Write the definition of a function powerTo, which receives two parameters. The first is a double and the second is an int. The function returns a double.

Write the definition of a function powerTo, which receives two parameters. The first is a double  and the second is an int. The function returns a double.

LANGUAGE: C++

CHALLENGE:

Write the definition of a function powerTo, which receives two parameters. The first is a double and the second is an int. The function returns a double.

If the second parameter is negative, the function returns 0. Otherwise, it returns the value of the first parameter raised to the power of the second.

SOLUTION:

double powerTo(double x, int y){ 
    if(y>0) return x*powerTo(x, y-1); 
    else if(y==0) return 1.0; 
    else return 0.0; 
}

Add comment