×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to calculate the sum of the series 1+(1+2) +(1+2+3) +(1+2+3+4) +...+(1+2+3+...+n)

Given a series: 1+(1+2) +(1+2+3) +(1+2+3+4) + ... +(1+2+3+...+n), and we have to find the sum of all elements using C program.
Submitted by Anshuman Das, on September 12, 2019

Problem statement

The series is: 1+(1+2) +(1+2+3) +(1+2+3+4) + ... +(1+2+3+...+n), we have to find out the sum up to N terms.

Solution

We know the sum of natural numbers up to n = (n(n-1))/2

If we simplify this we get, n(n+1)(2n+4)/12

If we put the number of terms in the above equation then we'll get the sum of the series up to that particular term.

C program to calculate the sum of the series 1+(1+2) +(1+2+3) +(1+2+3+4) +...+(1+2+3+...+n)

Now, let's see Program it using the using c program,

#include <stdio.h>

//function for creating the sum of the
//series up to Nth term

int series_sum(int n)
{
    return ((n * (n + 1) * (2 * n + 4)) / 12);
}

int main()
{
    int n;
    
    printf("Series:1+(1+2+)+(1+2+3)+...+(1+2+3+...+n)\n");
    printf("Want some up to N terms?\nEnter the N term:");
    scanf("%d", &n);

    printf("Sum is:%d", series_sum(n));

    return 0;
}

Output

Series:1+(1+2+)+(1+2+3)+...+(1+2+3+...+n)
Want some up to N terms?
Enter the N term:10
Sum is:220

C Sum of Series Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.