C++ program to calculate the sum of numbers from 1 to 100
The example programs will show you how to calculate the sum of numbers from 1 to 100 in C++.
Using for loop
#include <iostream>
using namespace std;
int main()
{
int sum=0;
for(int i=1; i<=100; i++)
{
// adding 1 to 100 numbers
sum=sum+i;
}
cout << "\n The sum of numbers from 1 to 100 is: "<<sum << endl;
return 0;
}
Output:
The sum of numbers from 1 to 100 is: 5050
Using while loop
#include <iostream>
using namespace std;
int main()
{
int i = 1, sum=0;
while (i <= 100) {
sum = sum+i;
i++;
}
cout << "\n The sum of numbers from 1 to 100 is: "<<sum << endl;
return 0;
}