by Sai gowtham

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;
}

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course