on
Italy
- Get link
- X
- Other Apps
#include
int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n){
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}
OutputEnter a positive integer: 5 15
sum() function is invoked from the same function. If n
is not equal to 0 then, the function calls itself passing argument 1
less than the previous argument it was called with. Suppose, n
is 5 initially. Then, during next function calls, 4 is passed to
function and the value of argument decreases by 1 in each recursive
call. When, n becomes equal to 0, the value of n is returned which is
the sum numbers from 5 to 1.sum(5) =5+sum(4) =5+4+sum(3) =5+4+3+sum(2) =5+4+3+2+sum(1) =5+4+3+2+1+sum(0) =5+4+3+2+1+0 =5+4+3+2+1 =5+4+3+3 =5+4+6 =5+10 =15
Comments
Post a Comment