Print Arithmetic progression & its sum - C

C program to print the following series & sum of series using for loop.

1 + 2 + 3 + .......... + N


Solution:

Mathematical formula for the sum of above series is: n*(n+1)/2

#include<stdio.h>
int main()
{
      int n,i;
      int sum=0;
      printf("1+2+3+...+N");
      printf("\n\nEnter Value of N : ");
      scanf("%d",&n);
      sum = (n * (n + 1)) / 2;  // Mathematical formula for sum of series.
      for(i =1;i <= n;i++)
      {
            if (i!=n)
                  printf("%d + ",i);
            else
                  printf("%d ",i);
      }
      printf("\n\nSum of Series = %d",sum);
      return 0;
}


Output:

arithmetic progression