In: Computer Science
Create a function powerTo which receives two parameters, a floating point number (the first parameter) and an integer (the second parameter). If the second parameter is nonnegative, the function returns the value of the first parameter raised to the power of the second parameter. Otherwise, if the second parameter is negative the function returns 0. For example powerTo(2,3)=8, and powerTo(2,-3)=0
CODE:
#include<stdio.h>
float powerTo(float x,int y)
{ int i;
float result=1.0;
if(y<0)
return 0;
else
{
for(i=0;i<y;i++)
{
result=result*x;
}
return result;
}
}
int main()
{
printf("%f",powerTo(2,3));
return 0;
}
OUTPUT:
If you have any doubts please COMMENT....
If you understand the answer please give THUMBS UP....