Lab 04
Version 1

 

  1. For the following sub-problems, start with a program that solves the first problem, then expand it to solve each successive problem. Make the program working for one problem before going on to the next.

    1. Ask the user for an integer and tell them whether it is negative or non-negative.

    2. Tell them, in addition, whether the number is odd or even.

    3. Ask the user for a second integer and tell them whether it is a multiple of the first, and, if it is, how many times the first it is.

    4. Tell the user which number is larger or if they are equal.

    5. Ask the user for a third integer and tell them whether the three numbers form a pythagorean triple (that is, if the sum of the squares of the first two equals the square of the third).

  2. Write a program which asks the user for their annual income, and tells them they are poor, comfortable, or stinking rich, depending on whether their income is less than $20,000, between $20,000 and $50,000, or greater than $50,000.

  3. Write a program which asks the user to type a number from 1 to 5 and responds with the cardinal version of the number (i.e. 1st, 2nd, 3rd, 4th, 5th) by printing the number they entered and then using an if-else-if statement to print the appropriate suffix.

    Notice that the last two cases use the same suffix. Try to capture that commonality in your program by not having separate cases for those two numbers.

    Print "out of range" if they enter any value other than 1 through 5.

  4. Write another program that does the same thing as the last one, but which uses a switch statement instead of an if statement.

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Tue, Sep 8, 1998 at 10:03:53 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_04/lab.html