Worksheet #1
Straight-line Programs
Instructions
Open the program SumOfTwoInts.cpp and start up
CodeWarrior. This program, as an example, adds two integers and prints the result.
Run SumOfTwoInts.cpp to see that it works.
Replace the body of this program (the contents of the main function) with statements that
will do each of the following. In each case, after making the change,
run the program to see that it works.
Exercises
- Write a program that will read three integers and print the sum.
- Write a program that will read three integers and print the product.
- Write a program that will read the length and width of a rectangle (as floats)
and print the area.
- Write a program that will read the radius of a circle and print its area. Use
3.14159 as the value of pi.
- Write a program that will read five numbers (floats) and print their average.
- Write a program that will read a first and a last name (two strings) and print them
in reverse order, separated by a comma.
- Write a program that will read a number and print its square root. Test it with negative
numbers as well as positive numbers.
- Write a program that will read a decimal number of hours (float) and print the whole
number of minutes contained in those hours. For example, if the input is 1.125 hours, then
the output will be 67 minutes.
Answers
SumOfThreeInts.cpp
ProductOfThreeInts.cpp
RectangleArea.cpp
CircleArea.cpp
Average.cpp
ReverseNames.cpp
SquareRoot.cpp
HoursToMins.cpp