Wyo C++ - Ch. 7 Worksheet #1 Name -

Answer the following ON LINED PAPER.

1. Show the output of the following program. (Draw a rectangle to represent the DOS window.)

#include <iostream.h>

int main( )
{
    int a = 3;
    int b = 4;
    int c = 8;
    if (a < b)
          cout << "a is less than b" << endl;
    else
          cout << "b is less than a" << endl;
          cout << "b is less than c" << endl;
    return 0;
}

2. What is the value of the following possible if statement control expressions where a = 1, b = 2 & c = 3. Write out the word true or false.

a/ (a < b || c = = 2)

b/ (a < b && (c = 2))

c/ (3 < b + a)

d/ (b < 1 || a > 0 && b < 0)

e/ (!(b > a))

f/ (b)

g/ (!a)

h/ (0 - c)

3. Write an if/else structure that displays the message "finished" if the value of the variable numberOfTimes is greater than or equal to 5. Otherwise, the message "not finished" displays.

4. Write one if/else statement that displays whether the value stored in the variable num is prime or not. You can assume that num is greater than 10 and that it is less than 30. Do not use brute force. You should use the mod operator ( % ) as in the following example.

if (num % 2 != 0 && num % 3 != 0)
{
   cout << num << " is not divisible by 2 or 3" << endl;
}
else
{
   cout << num << "is not prime since it is divisible by 2 and/or 3" << endl;
}

5. Evaluate the following expression where a = 5 and b = 6. Write the word true or false.

(a < b || (!b) && 6 >= b && (b < 3 || a))

6. Create an exercise like #2 with parts a/ through h/. Write your answers next to each exercise.