Visual C++ - Sum of 0 to N Numbers - n (n+1) / 2
Here is the Visual C++ program for the sum of 0 - N using the formula n (n+1) / 2.
Source Code
#include <iostream>
void main()
{
int N, sum = 0;
std::cout << "Program for sum of all numbers from 0 - N\n";
std::cout << "Enter N: ";
std::cin >> N;
sum = N * (N+1) / 2;
std::cout << "The sum of all numbers between 0 and " << N << " is: " << sum << "\n";
}
Output
Program for sum of all numbers from 0 - N
Enter N: 100
The sum of all numbers between 0 and 100 is: 5050
|