#include
/* We declare two functions. But they are defined later in the program */
int add(int n, int m);
int power(int n, int m);
int main( void )
{
int i, j, sum, p;
printf("Enter two positive integers: ");
scanf("%d %d", &i, &j);
sum = add(i, j); /* compute the sum of i and j recursively */
p = power(i,j); /* compute power i^j recursively */
printf("sum = %d power = %d\n", sum, p);
return(0);
}
/* This is an example of a recursive implementation of addition
We take the first argument and increment it by 1 as many times
as required according to the value of the second argument. This is
equivalent to computing the sum (n+m) as n + 1 + ... (m times) ... + 1.
*/
int add(int n, int m)
{
if ( m==0 ) /* base case */
return( n );
else /* recursive call */
return( add(n, m-1) + 1);
}
/* This is an example of a recursive implementation of power.
We mutiply the first argument with itself as many times as
indicated by the second argument to compute n^m = (n * ...(m times)... * n).
*/
int power(int n, int m)
{
if ( m==1 ) /* base case */
return( n );
else /* recursive call */
return( power(n,m-1) * n);
}