how do you do powers with a for loop?
Printable View
how do you do powers with a for loop?
Just loop n times and multiply val by itself:
-PreludeCode:int power ( int val, int n )
{
int ret = 1;
for ( int i = 0; i < n; i++ )
ret *= val;
return ret;
}
There is a function called exp();. This is found in math.h. The syntax is
exp(number,power);
I know this isn't a for loop, but it is a differrent method.