-
power recursion
Code:
#include <stdio.h>
#include <stdlib.h>
int power ( int, int );
int main()
{
int base, exp;
printf("?");
scanf( "%d%d", &base, &exp );
printf( "awn == %d\n", power( base, exp ) );
system("PAUSE");
return 0;
}
int power ( int base, int exp )
{
if ( exp == 1 )
return exp;
else
return ( power( base, exp -1 ) * base );
}
It uses a recursion function to find the power of int ^ itn ..Well the output is not right where is the problem i never did read the recursion part of the book and know i have to suffer :( ...
if i enter 2 ^ 4 it gives me 8 instead of 16 ???
-
Change this
Code:
if ( exp == 1 )
return exp;
to this
Code:
if ( exp == 1 )
return base;
}
because x^1 = x and x^0 = 1
-