How calculate the sum of numbers from 1 to 100 in C++

2 Answers

0 votes
#include <iostream>

int main() {
   int sum = 0;

   for (int i = 1; i <= 100; i++) {
        sum += i;
   }
   
   std::cout << sum;
}




/*
run:
 
5050
 
*/

 



answered Jun 25, 2021 by avibootz
0 votes
#include <iostream>

int main() {
   int i = 1, sum = 0;
   
   while (i <= 100) {
      sum += i;
      i++;
   }
   
   std::cout << sum;
}




/*
run:
 
5050
 
*/

 



answered Jun 25, 2021 by avibootz

Related questions

1 answer 208 views
1 answer 82 views
1 answer 120 views
1 answer 105 views
...