C Program Sum Series N Natural Numbers

By | March 11, 2020

C Program Sum Series N Natural Numbers

/*
  C program to calculate and print 
  sum of the series 1+2+3+4+...+n
  C program to print the sum of 
  all natural numbers from 1 to n.
  */
 
#include<stdio.h>
     
int main()
{
    int i,n,sum;
     
    printf("Enter the value of n: ");
    scanf("%d",&n);
     
    sum=0;
     
    /* loop to calculate sum from 1 to n*/
    for(i=1;i<=n;i++)
        sum= sum+ i;
     
    /*display the sum on screen*/
     
    printf("The Sum of the series 1+2+3+...+%d is: %d\n",n,sum);
     
    return 0;
}

Output

Enter the value of n: 10
The Sum of the series 1+2+3+…+10 is: 55

 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *