C program to find Sum of Series : 1+2+3+4+….+N using Recursion

By | 05.11.2016

Find sum of Series : 1+2+3+4+….+N


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.


Algorithm : : 


  • 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

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

2 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments