Sunday, January 15, 2012

sum using recursive function


/* A program to find the sum of given non-negative integer numbers using a recursive function */

#include<stdio.h>

int sum(int a)
{
    scanf("%d",&a);
    if(a<=0)
        return a; /*can return zero value as well*/
    else
        return(a+sum(a));
}
int main()

{
    int a;
    printf("enter numbers\n");
    printf("sum= %d",sum(a));
    return 0;

}

No comments:

Post a Comment