0
572views
Write a program for finding sum of series

1+2+3+4……upto n terms.

Subject : Structured Programming Approach

Title : Control Structures

Difficulty : Medium

1 Answer
0
0views

Program:

#include<stdio.h>

#include <conio.h>

void main() 

{

    int n,i;

    int sum=0;

    printf("Enter the n i.e. max value of series: ");

    scanf("%d",&n);

    sum = (n * (n + 1)) / 2;

    printf("Sum of the series: ");

    for (i =1;i <= n;i++) 

    {

        if (i!=n)

        printf("%d + ",i);

         else

        printf("%d = %d ",i,sum);

    }

    getch( );

}

Output:

Enter the n i.e. max value of series: 5

Sum of the series: 1 + 2 +3 + 4 + 5 = 15

Please log in to add an answer.