Calculate the sum of the integers from 1 to 10 with for loop - C++ Statement

C++ examples for Statement:for

Description

Calculate the sum of the integers from 1 to 10 with for loop

Demo Code

#include <iostream>
using namespace std;

int main() {/* www  . j  ava 2  s .  c o m*/
   unsigned int sum{0};
   unsigned int x{1};

   while (x <= 10) { // while x is less than or equal to 10
      sum += x; // add x to sum
      ++x; // increment x
   } 

   cout << "The sum is: " << sum << endl;
}

Result


Related Tutorials