Sum of n numbers
Algorithm
Input N (The numbers from 1 to N will be added) Output Sum of first n numbers complexity O(1) Sum_Num(N) 1 Sum=N*(N+1)/2; 2 Print sum;
Algorithm Description
This algorithm adds all number from 1 to N. To add first N number program uses the formula sum=N*(N+1)/2;
C Code
//To find the sum of 1+2+3....+n terms
//Input : Enter any positive integer n
#include< stdio.h>
#include< conio.h>
int main()
{
int sum,n;
printf("Enter a number upto which sum is required ");
scanf("%d",&n);
sum=n*(n+1)/2;
printf("Sum from 1 to %d is %d",n,sum);
}