Software & Finance





Turbo C - Sum of 0 to N Numbers - n (n+1) / 2





Here is the Turbo C program for the sum of 0 - N using the formula n (n+1) / 2.

 


Source Code


#include <stdio.h>

 

void main()

{

    int N, sum = 0;

    printf("Program for sum of all numbers from 0 - N\n");

 

    printf("Enter N: ");

    scanf("%d", &N);

   

    sum = N * (N+1) / 2;

   

    printf("The sum of all numbers between 0 and %d is: %d", N, sum);

}

Output


 

Program for sum of all numbers from 0 - N

Enter N: 100

The sum of all numbers between 0 and 100 is: 5050