/* lab08.txt CIS 150 6/21/05 David Klick Solution to lab #8. */ #include using std::cout; using std::cin; int main(void) { int num, i, sum, x; // step 1 // ask the user for a number // display "Hello, world!" that many times cout << "How many times should I display my message? "; cin >> num; for (i=1; i<=num; i++) { cout << "Hello, world!\n"; } cout << '\n'; // step 2 // ask the user how many numbers they want // to enter to have summed do { cout << "How many numbers would you like to enter: "; cin >> num; if (num < 0) { cout << "Error: Number must be positive\n"; } } while (num < 0); // now get the numbers, add them up, and display sum sum = 0; for (i=1; i<=num; i++) { cout << "Enter number: "; cin >> x; sum += x; } cout << "The sum is " << sum << "\n\n"; // step 3 // display whether numbers are even or odd // until user enters -999 cout << "Enter an integer (-999 to quit): "; cin >> num; while (num != -999) { if (num % 2 == 0) { cout << num << " is even\n"; } else { cout << num << " is odd\n"; } cout << "Enter an integer (-999 to quit): "; cin >> num; } cout << '\n'; // step 4 // display the odd numbers from 1 to 10 using all three loops for (i=1; i<=10; i+=2) { cout << i << " "; } cout << '\n'; i = 1; while (i <= 10) { cout << i << " "; i+=2; } cout << '\n'; i = 1; do { cout << i << " "; i+=2; } while (i <= 10); cout << '\n'; return 0; }