Write a C program to find sum of Series : 1+2+3+4+….+N. Here’s a Simple program to find sum of Series : 1+2+3+4+….+N by creating Recursive and Iterative Functions in C Programming Language.
First printf statement will ask the user to enter any integer value and the scanf statement will assign the user entered value to Number variable.
Next, we used For Loop to iterate between 1 and user entered value.
Within the For loop, we calculated the sum.
In the above example, user entered value is 5 it means, 1 + 2 + 3 + 4 + 5 = 15
This program allows the user to enter any integer Value. Using the Recursive and Iterative Functions we will calculate the sum of N natural Numbers.
Below is the source code for C program to find sum of Series : 1+2+3+4+….+N which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C program to find sum of Series : 1+2+3+4+....+N */
#include<stdio.h>
int series(int n);
int rseries(int n);
int main( )
{
int n;
printf("Enter number of terms : ");
scanf("%d", &n);
printf("\b\b Using Recursion :: \n");
printf("\b\b = %d\n", series(n)); /* \b to erase last +sign */
printf("\n\b\b Using Recursion :: \n");
printf("\b\b = %d\n\n\n", rseries(n));
return 0;
}/*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 : :
/* C program to find sum of Series : 1+2+3+4+....+N */
Enter number of terms : 15
Using Recursion ::
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 = 120
Using Recursion ::
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 = 120
Algorithm : :
This program allows the user to enter any integer Value. Using the Recursive and Iterative Functions we will calculate the sum of N natural Numbers.
Below is the source code for C program to find sum of Series : 1+2+3+4+….+N which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
Output : :
need an explanation for this answer? contact us directly to get an explanation for this answer