tag:blogger.com,1999:blog-67894397822716641372017-01-12T04:04:36.389-08:00Basic Programming ExercisesJunnoreply@blogger.comBlogger7125tag:blogger.com,1999:blog-6789439782271664137.post-43711955721417032502010-02-08T19:10:00.000-08:002010-06-15T19:43:20.345-07:00Exercise Set#7<b>Exercise 1 --- Adding up Squares and Cubes</b><br />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:<br /><br />Upper Limit: 5<br />The sum of Squares is 55<br />The sum of Cubes is 225<br />Do this by using just one loop.<br /><br /><b>Exercise 2 --- Power of a number</b><br />Write a program that computes XN.<br />XN = X * X * X * ... * X<br />--------------------<br />X: 1.3<br />N: 5<br /><br />1.3 raised to the power 5 is: 3.71293<br /><br />-------<br /><br />X: 5.6<br />N: -3<br /><br />N must be a positive integer.<br /><br /><b>Exercise 3 --- Wedge of Stars</b><br />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:<br /><br />Initial number of stars: 7<br /><br />*******<br />******<br />*****<br />****<br />***<br />**<br />*<br /><br /><b>Exercise 4 --- Holiday Tree</b><br />Write a program that will write a tree made of stars on the terminal:<br /><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> *</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> ***</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> *****</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> *******</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> *********</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> ***********</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> *************</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">***************</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">Add the following style to create monospaced characters</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"></span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"></span><div style="font-family: Courier;"><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">// put your tree here</span><br /><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"></span></div><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"></span>Junnoreply@blogger.com4tag:blogger.com,1999:blog-6789439782271664137.post-68318673605905467522010-02-08T18:43:00.000-08:002010-02-08T18:43:23.649-08:00Exercise Set#6<b>Exercise 1 - Start to End</b><br />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.<br /><br />Start: 5<br />End: 9<br /><br />5<br />6<br />7<br />8<br />9<br /><br /><b>Exercise 2 - N Times Words</b><br />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:<br /><br />Word: Hello<br /><br />Hello<br />Hello<br />Hello<br />Hello<br />Hello<br /><br /><b>Exercise 3 - Thirty Characters</b><br />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:<br /><br />First word: turtle<br />Second word: 153<br /><br />turtle....................153Junnoreply@blogger.com3tag:blogger.com,1999:blog-6789439782271664137.post-55015366607625496532010-02-08T18:40:00.000-08:002011-11-29T01:29:31.128-08:00Exercise Set#5<b>Exercise 1 --- Check Charge</b><br />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.<br /><br /><b>Exercise 2 --- Tire Pressure</b><br />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.<br /><br />Right front pressure: 38<br />Left front pressure: 38<br />Right rear pressure: 42<br />Left rear pressure: 42<br /><br />Inflation is OK<br /><br /><b>Exercise 3 --- More Tire Pressure</b><br />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.<br /><br />Right front pressure: 32<br />Warning: pressure is out of range<br /><br />Left front pressure: 32<br />Warning: pressure is out of range<br /><br />Right rear pressure: 42<br />Left rear pressure: 42<br /><br />Inflation is BAD<br /><br />If there have been any warnings, write out a final error message.<br /><br /><b>Exercise 4 --- The Pressure is Building</b><br />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.<br /><br />Right front pressure: 35<br />Left front pressure: 37<br />Right rear pressure: 41<br />Left rear pressure: 44<br /><br />Inflation is OKJunnoreply@blogger.com2tag:blogger.com,1999:blog-6789439782271664137.post-6165755226822218362010-02-07T23:22:00.000-08:002010-02-07T23:22:45.492-08:00Exercise Set#4<b>Exercise 1 --- Internet Delicatessen</b><br />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.<br /><br />The item: Tuna Salad<br />Enter the price: 450<br />Overnight delivery (0==no, 1==yes): 1<br /><br />Invoice:<br />Tuna Salad 4.50<br />shipping 7.00<br />total 11.50<br /><br /><b>Exercise 2 --- Steam Engine Efficiency</b><br />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:<br /><br />efficiency = 1 - Tair / Tsteam<br />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.<br /><br /><b>Exercise 3 --- Microwave Oven</b><br />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.<br /><br />Write a program that accepts the number of items and the single-item heating time. The program then writes out the recommended heating time.<br /><br />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.<br /><br /><b>Exercise 4 --- Fantasy Game</b><br />In a new role-playing fantasy game players get to design their character by picking a point value for each of three characteristics:<br /><br />Strength, from 1 to 10<br />Health, from 1 to 10<br />Luck, from 1 to 10<br /><br />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<br />Welcome to Ever's Quest<br />Enter the name of your character: Sunny<br />Strength (1-10): 8<br />Health (1-10): 4<br />Luck (1-10): 6<br /><br />You have give your character too many points! Default values have been assigned:<br /><br />Chortle, strength: 5, health: 5, luck: 5Junnoreply@blogger.com2tag:blogger.com,1999:blog-6789439782271664137.post-43955138191980744622010-02-07T23:16:00.000-08:002010-02-07T23:16:07.850-08:00Exercise Set#3<b>Exercise 1 --- Discount Prices</b><br />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.<br />Amount of purchases: 2000<br />Discounted price: 1800<br /><br /><b>Exercise 2 --- Order Checker</b><br />Bob's Discount Bolts charges the following prices:<br />5 cents per bolt<br />3 cents per nut<br />1 cent per washer<br />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.<br />Number of bolts: 12<br />Number of nuts: 8<br />Number of washers: 24<br /><br />Check the Order<br /><br />Total cost: 108<br /><br /><b>Exercise 3 --- Last Chance Gas</b><br />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:<br /><br />The capacity of the gas tank, in gallons<br />The indication of the gas gauge in percent (full= 100, three quarters full = 75, and so on)<br />The miles per gallon of the car.<br />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.<br />Tank capacity: 12<br />Gage reading: 50<br />Miles per gallon: 30<br />Get Gas!<br /><br /><b>Exercise 4 --- Pie Eating Contest</b><br />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.<br /><br /><b>Exercise 5 --- Ground Beef Value Calculator</b><br />Different packages of ground beef have different percentages of fat and different costs per pound. Write a program that asks the user for:<br /><br />The price per pound of package "A"<br />The percent lean in package "A"<br />The price per pound of package "B"<br />The percent lean in package "B"<br />The program then calculates the cost per pound of lean (non-fat) beef for each package and writes out which is the best value.<br />Price per pound package A: 2.89<br />Percent lean package A: 85<br />Price per pound package B: 3.49<br />Percent lean package B: 93<br /><br />Package A cost per pound of lean:3.4<br />Package B cost per pound of lean:3.752688<br />Package A is the best value<br />Assume that the two packages will not come out equal in value.<br /><br /><b>Exercise 5 --- Y2K Problem Detector</b><br />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.<br /><br />Year of Birth: 62<br />Current year: 99<br />Your age: 37<br /><br />----- another input for the program --------<br />Year of Birth: 62<a href="javascript:void(0)">Publish Post</a><br />Current year: 00<br />Your age: 38Junnoreply@blogger.com2tag:blogger.com,1999:blog-6789439782271664137.post-32173614252284605432010-02-07T23:07:00.000-08:002010-02-16T22:05:17.242-08:00Exercise Set#2<b>Exercise 1 --- Power Consumption</b><br />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:<br /><br />Cost per kilowatt-hour in cents: 8.42<br />Kilowatt-hours used per year: 653<br />Annual cost: 54.9826<br /><br /><b>Exercise 2 --- Speed</b><br />When a brick is dropped from a tower, it falls faster and faster until it hits the earth. The speed v is given by<br />v = 1/2 g t2<br />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.<br /><br />Number of seconds: 5.4<br />Speed of the brick: 469.092 feet per second<br /><br /><b>Exercise 3 - Logarithm</b><br />The base 2 logarithm of a number is defined by:<br />log2 X = n if 2n = X<br />For example<br /><br />25 = 32, so <br />log2 32 = 5<br />210 = 1024, so<br />log2 1024 = 10.<br /><br />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:<br /><br />log2 X = (loge X) / (loge 2)<br />Here, loge X is the natural logarithm of X.<br /><br />Base 2 log of 998.65 is 9.963835330516641<br /><br />Exercise 4 - Harmonic<br />The harmonic mean of two numbers is given by:<br />H = 2 / ( 1/X + 1/Y )<br />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.<br />X: 12<br />Y: 16<br />Arithmetic mean: 14.0<br />Harmonic mean: 13.714285714285715Junnoreply@blogger.com1tag:blogger.com,1999:blog-6789439782271664137.post-60599347697647997972010-01-11T23:33:00.000-08:002010-01-11T23:34:03.025-08:00Exercise Set#1<b>Exercise 1 --- Area of a Circle</b><br /><div>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()<br /></div><div><br /><b>Exercise 2 --- Cents to Dollars</b><br /></div><div>Write a program that reads in a number of cents. The program will write out the number of dollars and cents, like this:<br />Input the cents: 324 That is 3 dollars and 24 cents.<br />Review the modulo operator % if you are unsure how to proceed<br /><br /><b>Exercise 3 --- Correct Change</b><br />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:<br />One dollar fits into 163, leaving 63 cents.<br />Two quarters fit into 63 cents, leaving 13 cents.<br />One dime fits into 13 cents, leaving 3 cents.<br />No nickels are needed.<br />Three cents are left.Your change is : 1 dollar, two quarters, one dime, and three cents.<br /><br />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.<br /><br /></div><div><span class="Apple-style-span" style="font-weight: bold;">Exercise 4 --- Ohm's Law</span><br />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:<br /><br />I = V/R<br /><br />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.<br /></div>Junnoreply@blogger.com4