Combine the statements that you wrote in Exercise 3.4 into a program that calculates and prints the sum of the integers from 1 to 10. Use the while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.
What will be an ideal response?
```
// Calculate the sum of the integers from 1 to 10.
#include
using namespace std;
int main()
{
int sum; // stores sum of integers 1 to 10
int x; // counter
x = 1; // count from 1
sum = 0; // initialize sum
while ( x <= 10 ) // loop 10 times
{
sum += x; // add x to sum
++x; // increment x
} // end while
cout << "The sum is: " << sum << endl;
} // end main
```
You might also like to view...
If you have the following variable declaration in your program,
const int SIZE=34; then which of the following statements are legal? a. SIZE ++; b. x = SIZE--; c. cout << SIZE; d. cin >> SIZE;
All layout managers implement the interface ________.
a. LayoutInterface b. InterfaceLayoutManager c. LayoutManagerInterface d. LayoutManager
Recursion can be used to
A) compute factorials. B) find the greatest common divisor of two integers (GCD). C) program things that cannot be programmed without recursion. D) All of the above E) Both A and B, but not C
Some programmers prefer not to use protected access, because they believe it breaks the encapsulation of the base class. Discuss the relative merits of using protected access vs. using private access in base classes.
What will be an ideal response?