About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C Program to calculate sum of numbers 1 to N using recursion

#include
#include
int add(int);
//-------------------------------------
void main()
{
int i,num;
int sum;
clrscr();
printf("Input a number : ");
scanf("%d",&num);
sum=add(num);
printf("\nSum of Number From 1 to %d :%d",num,sum);
}
//---------------------------------------
int add(int m)
{
int sum;
if(m==1)
    return(1);
else
   sum=m+add(m-1);
return(sum);
}

Output :
Input a number : 5
Sum of Number From 1 to 5 :15

No comments: