Write a C program to calculate a series of integer numbers as 1+2+3+......+n

Here 1+2+3+…….+n is a simple series where each number can be added using a single loop. At first take a variable SUM and initialize it by Zero. Then add each number of the series successively. Then print the SUM.


//1+2+3+..............+n

#include <stdio.h>

main(){
  int i,n,SUM=0;
  printf("Insert the last number: ");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
       SUM=SUM+i;
  printf("Total: %d",SUM);

  return 0;
 }

No comments:

Post a Comment