Program to display and find out the sum of series by the iterative and recursive method Series : 1 + 2 + 3 + 4 + 5 +.......

C Programming Language Recursion in c (Article) Recursion in c (Program)

601

Program:

#include<stdio.h>
int series(int n);
int rseries(int n);
main( )
{
int n;
printf("Enter number of terms : ");
scanf("%d", &n);
printf("\b\b = %d\n", series(n)); /* \b to erase last +sign */
printf("\b\b = %d\n\n\n", rseries(n));
}/*End of main()*/

/*Iterative function*/
int series(int n)
{
int i, sum=0;
for(i=1; i<=n; i++)
{
printf("%d + ", i);
sum+=i;
}
return sum;
}/*End of series()*/

/*Recursive function*/
int rseries(int n)
{
int sum;
if(n == 0)
	return 0;
sum = (n + rseries(n-1));

printf("%d + ",n);
return sum;
}/*End of rseries()*/

Output:

Enter number of terms : 10
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10  = 55
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10  = 55


Press any key to continue . . .

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.