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

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

👁 691 Views
💻 Practical Program
📘 Step by Step Learning
Program to display and find out the sum of series by the iterative and recursive method

Program Code

#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

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.