Sum of first N numbers Program using C syntax

This ” Sum Of First N Numbers Program in C Language” post explains how to find sum of first n numbers using C language syntax.


#include<stdio.h>
void main()
{
int limit , i, sum=0 ;
printf(“Enter the Limit : “);
scanf(“%d”, &limit);
for(i=1 ; i<=limit ; i++)
{
sum = sum + i ;
}
printf(“Sum of first %d numbers is : %d”,limit, sum);
}


#include<stdio.h>
void main()
{
int n , i, sum=0 ;
printf(“Enter the Limit : “);
scanf(“%d”, &n);
sum = (n*(n+1))/2 ;
printf(“Sum of first %d numbers is : %d”,n, sum);
}


 

Share this