Navigation

Saturday, 28 February 2015

Sum of first n number using recursion

int sum(int a);
void main()
{
   int x,y;
   clrscr();
   printf("Enter any positive number upto which you want to sum::=>");
   scanf("%d",&y);
   printf("The sum of first %d integers = %d\n",y,sum(y));
   getch();
}
int sum(int y)
{
   if(y==0)
   return 0;
   else
   return(y+sum(y-1));
}

No comments:

Post a Comment