Homework 8: Due Tuesday, October 10th

Read Chapter 5, sections 5.1 through 5.3. Complete the following problems:

What is printed by each of the following? Assume that all variables are declared as int.

1.  i = 1;                       2.  i = 2;
    while (i < 10)                   while (i < 10)
       i = i + 1;                       i++;
    cout << i << endl;               cout << i << endl;

3.  i = 10;                      4.  i = 2;
    while (i < 10)                   while (i < 15)
         i = i + 1;                     i = i + 2;
    cout << i << endl;               cout << i << endl;

5.  total = 4;
    i = 2;
    while (i < 5)
    {
       total = total * i;
       i = i + 1;
    }
    cout << "The total is " << total << endl;

6.  i = 0;                     7.  i = 0;
    while (i < 5)                  if (i < 5)
    {                              {
       cout << i << endl;              cout << i << endl;
       i = i + 1;                      i = i + 1;
    }                              }



8.  i = 15;                    9.  a = 3;
    while ( i == 25)               b = 100;
    {                              while (b / a > 5)
       cout << i << endl;             if (b - a > 90)
       i = i + 1;                     {
    }                                    a = a + 5;
                                         cout << a << endl;
                                      }
                                      else
                                      {
                                         b = b / 2;
                                         cout << b << endl;
                                      }

10. Use a while loop to write a program segment that:

      1.  reads a number input by the user and stores the number in a
          variable called limit.
      2.  prints all the odd numbers between 1 and limit

11. Use a while loop to write a program segment that:

      1.  reads a number input by the user and stores the number in a
          variable called limit.
      2.  adds up all the numbers between 1 and limit
      3.  prints the sum

12. Use a while loop to write a program segment that:

      1.  reads a number input by the user and stores the number in a
          variable called limit.
      2.  adds up all the even numbers between 2 and limit
      3.  prints the sum

Remember to type your answers and to make two copies of each homework assignment. One copy is submitted to me and the other you keep to use in the group discussions.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2004