Sunday, 19 October 2014

Combine the statements that you wrote in Exercise 2.4 into a program that calculates and prints the sum of the integers from 1 to 10. Use the while structure to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.

Leave a Comment


Combine the statements that you wrote in Exercise 2.4 into a program that calculates and prints the sum of the integers from 1 to 10. Use the while structure to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.

Solution


#include <iostream>
using namespace std;

int main()
{
      int sum, x;
      x = 1;
      sum = 0;
      while( x <= 10 )
      {
             sum += x;
             ++x;
      }
      cout << "The sum is: " << sum << endl;
      return 0;
}
If You Enjoyed This, Take 5 Seconds To Share It