on Monday, February 8, 2010
Exercise 1 --- Adding up Squares and Cubes
Write a program that adds up the squares and adds up the cubes of integers from 1 to N, where N is entered by the user:

Upper Limit: 5
The sum of Squares is 55
The sum of Cubes is 225
Do this by using just one loop.

Exercise 2 --- Power of a number
Write a program that computes XN.
XN = X * X * X * ... * X
--------------------
X: 1.3
N: 5

1.3 raised to the power 5 is: 3.71293

-------

X: 5.6
N: -3

N must be a positive integer.

Exercise 3 --- Wedge of Stars
Write a program that will write out a wedge of stars. The user will enter the initial number of stars, and the program will write out lines of stars where each line has one few star than the previous line:

Initial number of stars: 7

*******
******
*****
****
***
**
*

Exercise 4 --- Holiday Tree
Write a program that will write a tree made of stars on the terminal:

       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************


Add the following style to create monospaced characters




// put your tree here
Exercise 1 - Start to End
Write a program that asks the user for a starting value and an ending value and then writes all the integers (inclusive) between those two values.

Start: 5
End: 9

5
6
7
8
9

Exercise 2 - N Times Words
Write a program that asks the user to enter a word. The program will then repeat word for as many times as it has characters:

Word: Hello

Hello
Hello
Hello
Hello
Hello

Exercise 3 - Thirty Characters
Write a program that asks the user to enter two words. The program then prints out both words on one line. The words will be separated by enought dots so that the total line length is 30:

First word: turtle
Second word: 153

turtle....................153
Exercise 1 --- Check Charge
A bank has the following rule: if a customer has more than $1000 dollars in their checking account or more than $1500 dollars in their savings account, then there is no service charge for writing checks. Otherwise there is a $0.15 charge per check. Write a program that asks for the balance in each account and then writes out the service charge.

Exercise 2 --- Tire Pressure
The front tires of a car should both have the same pressure. Also, the rear tires of a car should both have the same pressure (but not neccessarily the same pressure as the front tires.) Write a program that accepts the pressure of the four tires and writes a message that says if the inflation is OK or not.

Right front pressure: 38
Left front pressure: 38
Right rear pressure: 42
Left rear pressure: 42

Inflation is OK

Exercise 3 --- More Tire Pressure
Its not enough that the pressures are the same in the tires, but the pressures must also be within range. Modify the program in exercise 1 so that it also checks that each tire has a pressure between 35 and 45.

Right front pressure: 32
Warning: pressure is out of range

Left front pressure: 32
Warning: pressure is out of range

Right rear pressure: 42
Left rear pressure: 42

Inflation is BAD

If there have been any warnings, write out a final error message.

Exercise 4 --- The Pressure is Building
Tires don't have to have exactly the same pressure. Modify the program for exercise 2 so that the front tires can be within 3 psi of each other, and the rear tires can be within 3 psi of each other.

Right front pressure: 35
Left front pressure: 37
Right rear pressure: 41
Left rear pressure: 44

Inflation is OK
on Sunday, February 7, 2010
Exercise 1 --- Internet Delicatessen
Sam and Ella's Delicatessen wants a program to take orders from the internet. The program will accept item the user wants, its price, and will ask if the user wants overnight shipping. Regular shipping for items under $10 is $2.00; for items $10 or more shipping is $3.00. For overnight delivery add $5.00.

The item: Tuna Salad
Enter the price: 450
Overnight delivery (0==no, 1==yes): 1

Invoice:
Tuna Salad 4.50
shipping 7.00
total 11.50

Exercise 2 --- Steam Engine Efficiency
The maximum possible efficiency of a steam engine depends on the temperature of the steam in the boiler and the temperature of the outside air:

efficiency = 1 - Tair / Tsteam
where Tair is the air temperature and Tsteam is the steam temperature. The temperatures are give in degrees above absolute zero. Normal air temperature is about 300oK. Boiling is 373oK. Write a program that accept  air temperature and the steam temperature and writes out the maximum possible efficiency of a steam engine. However, if the steam temperature is less than 373oK there is no steam, so the efficiency is zero.

Exercise 3 --- Microwave Oven
A microwave oven manufacturer recommends that when heating two items, add 50% to the heating time, and when heating three items double the heating time. Heating more than three items at once is not recommended.

Write a program that accepts the number of items and the single-item heating time. The program then writes out the recommended heating time.

Hint: do this with four successive single-branch if statements each of which tests for one of the four cases: 1 item, 2 items, 3 items, more than three items.

Exercise 4 --- Fantasy Game
In a new role-playing fantasy game players get to design their character by picking a point value for each of three characteristics:

Strength, from 1 to 10
Health, from 1 to 10
Luck, from 1 to 10

Write a program that accepts a name for the character and asks for the point value of for each of the three characteristics. However, the total points must be less than 15. If the total exceeds 15, then 5 points are assigned to each characteristic
Welcome to Ever's Quest
Enter the name of your character: Sunny
Strength (1-10): 8
Health (1-10): 4
Luck (1-10): 6

You have give your character too many points! Default values have been assigned:

Chortle, strength: 5, health: 5, luck: 5
Exercise 1 --- Discount Prices
During a special sale at a store, a 10% discount is taken on purchases over $10.00. Write a program that asks for the amount of purchases, then calculates the discounted price.
Amount of purchases: 2000
Discounted price: 1800

Exercise 2 --- Order Checker
Bob's Discount Bolts charges the following prices:
5 cents per bolt
3 cents per nut
1 cent per washer
Write a program that asks the user for the number of bolts, nuts, and washers in their purchase and then calculates and prints out the total. As an added feature, the program checks the order. It is usually a mistake if there are more bolts than nuts. In this case the program writes out "Check the Order." Otherwise the program writes out "Order is OK." In either case the total price is written out.
Number of bolts: 12
Number of nuts: 8
Number of washers: 24

Check the Order

Total cost: 108

Exercise 3 --- Last Chance Gas
Al's Last Chance Gas station sits on route 190 on the edge of Death Valley. There is no other gas station for 200 miles. You are to write a program to help drivers decide if they need gas. The program asks for:

The capacity of the gas tank, in gallons
The indication of the gas gauge in percent (full= 100, three quarters full = 75, and so on)
The miles per gallon of the car.
The program then writes out "Get Gas" or "Safe to Proceed" depending on if the car can cross the 200 miles with the gas remaining in the tank.
Tank capacity: 12
Gage reading: 50
Miles per gallon: 30
Get Gas!

Exercise 4 --- Pie Eating Contest
At the State Fair Pie Eating Contest all contestants in the heavyweight division must weigh within 30 pounds of 250 pounds. Write a program that asks for a contestant's weight and then says if the contestant is allowed in the contest.

Exercise 5 --- Ground Beef Value Calculator
Different packages of ground beef have different percentages of fat and different costs per pound. Write a program that asks the user for:

The price per pound of package "A"
The percent lean in package "A"
The price per pound of package "B"
The percent lean in package "B"
The program then calculates the cost per pound of lean (non-fat) beef for each package and writes out which is the best value.
Price per pound package A: 2.89
Percent lean package A: 85
Price per pound package B: 3.49
Percent lean package B: 93

Package A cost per pound of lean:3.4
Package B cost per pound of lean:3.752688
Package A is the best value
Assume that the two packages will not come out equal in value.

Exercise 5 --- Y2K Problem Detector
Write a program that asks a user for their birth year encoded as two digits (like "62") and for the current year, also encoded as two digits (like "99"). The program is to correctly write out the users age in years.

Year of Birth: 62
Current year: 99
Your age: 37

----- another input for the program --------
Year of Birth: 62Publish Post
Current year: 00
Your age: 38
Exercise 1 --- Power Consumption
Write a program that calculates the annual cost of running an appliance given the cost per kilowatt-hour and the number of killowatt-hours the appliance uses in a year:

Cost per kilowatt-hour in cents: 8.42
Kilowatt-hours used per year: 653
Annual cost: 54.9826

Exercise 2 --- Speed
When a brick is dropped from a tower, it falls faster and faster until it hits the earth. The speed v is given by
v = 1/2 g t2
Here v is the speed in feet per second, t is the time in seconds, and g is 32.174. Given the number of seconds and the script will display the speed.

Number of seconds: 5.4
Speed of the brick: 469.092 feet per second

Exercise 3 - Logarithm
The base 2 logarithm of a number is defined by:
log2 X = n if 2n = X
For example

25 = 32, so
log2 32 = 5
210 = 1024, so
log2 1024 = 10.

Write a program that accepts a number and outputs its base 2 logarithm. This problem would be easy, but the Math package does not have a base 2 logarithm method. Instead you have to do this:

log2 X = (loge X) / (loge 2)
Here, loge X is the natural logarithm of X.

Base 2 log of 998.65 is 9.963835330516641

Exercise 4 - Harmonic
The harmonic mean of two numbers is given by:
H = 2 / ( 1/X + 1/Y )
This is sometimes more useful than the more usual average of two numbers. Write a program that accepts two numbers and writes out both the usual average (the arithmetic mean) and the harmonic mean.
X: 12
Y: 16
Arithmetic mean: 14.0
Harmonic mean: 13.714285714285715
on Monday, January 11, 2010
Exercise 1 --- Area of a Circle
Write a program that calculates the area of a circle from the radius. The radius will be an integer read in from the keyboard. You will need to use the function pi()

Exercise 2 --- Cents to Dollars
Write a program that reads in a number of cents. The program will write out the number of dollars and cents, like this:
Input the cents: 324 That is 3 dollars and 24 cents.
Review the modulo operator % if you are unsure how to proceed

Exercise 3 --- Correct Change
When cashiers in a store give you change they try first try to "fit" dollars into the amount you get back, then try to "fit" quarters (25 cent coins) into what is left over, they try to "fit" dimes (10 cent coins) into what is now left over, then try to "fit" nickels (5 cent coins) into what is left, and finally are left with a few odd cents. For example, say that your change is 163 cents:
One dollar fits into 163, leaving 63 cents.
Two quarters fit into 63 cents, leaving 13 cents.
One dime fits into 13 cents, leaving 3 cents.
No nickels are needed.
Three cents are left.Your change is : 1 dollar, two quarters, one dime, and three cents.

Write a program that reads change due to a user (in cents) and writes out how many dollars, quarters, dimes, nickels, and pennies she is due. If you are stuck, it will help to do an example problem with paper and pencil.

Exercise 4 --- Ohm's Law
Ohm's law relates the resistance of a electrical device (like a heater) to the electric current flowing through the device and the voltage applied to it. The law is:

I = V/R

Here, V is the voltage (measured in volts), I is the current (measured in amps), and R is the resistance (measured in ohms.) Write a program that asks the user for the voltage and the resistance of a device. The program will then write out the current flowing through it.