Practice Programs

Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.

  1. Write a program to score the paper-rock-scissor game. Each of two users types in either P, R, or S. The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase as well as uppercase letters. Your program should include a loop that lets the user play again until the user says she or he is done.

  2. Write a program to compute the interest due, total amount due, and the minimum payment for a revolving credit account. The program accepts the account balance as input, then adds on the interest to get the total amount due. The rate schedules are the following: The interest is 1.5 percent on the first $1,000 and 1 percent on any amount over that. The minimum payment is the total amount due if that is $10 or less; otherwise, it is $10 or 10 percent of the total amount owed, whichever is larger. Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.

  3. Write an astrology program. The user types in a birthday, and the program responds with the sign and horoscope for that birthday. The month may be entered as a number from 1 to 12. Then enhance your program so that if the birthday is only one or two days away from an adjacent sign, the program announces that the birthday is on a “cusp” and also outputs the horoscope for that nearest adjacent sign. This program will have a long multiway branch. Make up a horoscope for each sign. Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.

    The horoscope signs and dates are:

    Aries March 21–April 19
    Taurus April 20–May 20
    Gemini May 21–June 21
    Cancer June 22–July 22
    Leo July 23–August 22
    Virgo August 23–September 22
    Libra September 23–October 22
    Scorpio October 23–November 21
    Sagittarius November 22–December 21
    Capricorn December 22–January 19
    Aquarius January 20–February 18
    Pisces February 19–March 20

  4. Horoscope Signs of the same Element are most compatible. There are 4 Elements in astrology, and 3 Signs in each: FIRE (Aries, Leo, Sagittarius), EARTH (Taurus, Virgo, Capricorn), AIR (Gemini, Libra, Aquarius), WATER (Cancer, Scorpio, Pisces).

    According to some astrologers, you are most comfortable with your own sign and the other two signs in your Element. For example, Aries would be most comfortable with other Aries and the two other FIRE signs, Leo and Sagittarius.

    Modify your program from Practice Program 3 to also display the name of the signs that will be compatible for the birthday.

  5. Write a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …).

    One way to solve this problem is to use a doubly nested loop. The outer loop can iterate from 3 to 100 while the inner loop checks to see if the counter value for the outer loop is prime. One way to see if number n is prime is to loop from 2 to n 21 and if any of these numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n 21 evenly divides n, then n must be prime. (Note that there are several easy ways to make this algorithm more efficient.)

  6. Buoyancy is the ability of an object to float. Archimedes’ principle states that the buoyant force is equal to the weight of the fluid that is displaced by the submerged object. The buoyant force can be computed by

    F b=V×γ

    where Fb is the buoyant force, V is the volume of the submerged object, and y is the specific weight of the fluid. If Fb is greater than or equal to the weight of the object, then it will float, otherwise it will sink.

    Write a program that inputs the weight (in pounds) and radius (in feet) of a sphere and outputs whether the sphere will sink or float in water. Use y = 62.4 lb/ft3 as the specific weight of water. The volume of a sphere is computed by (4/3)πr3.

  7. Write a program that finds the temperature that is the same in both Celsius and Fahrenheit. The formula to convert from Celsius to Fahrenheit is

    Fahrenheit= (9×Celsius) 5+32

    Your program should create two integer variables for the temperature in Celsius and Fahrenheit. Initialize the temperature to 100 degrees Celsius. In a loop, decrement the Celsius value and compute the corresponding temperature in Fahrenheit until the two values are the same.

    Since you are working with integer values, the formula may not give an exact result for every possible Celsius temperature. This will not affect your solution to this particular problem.