Exercise Set#2

on Sunday, February 7, 2010
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

1 comments:

ŇÃŜĮŘ ÃββÃŜ said...

Nys example
For More programming exercises ,free e books and free projects visit this blog
programmingexercises

Post a Comment