PF-Worksheet1-Fall 2021
PF-Worksheet1-Fall 2021
Happy Coding
1. A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges
an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum
charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours
at a time. Write a program that will calculate and print the parking charges for each of 3 customers
who parked their cars in this garage yesterday. You should enter the hours parked for each
customer. Your program should print the results in a neat tabular format and should calculate and
print the total of yesterday's receipts. The program should use the function double
calculateCharges( double hours ) to determine the charge for each customer. Your outputs
should appear in the following format:
2. Magic Dates: The date June 10, 1960 is special because when we write it in the following format,
the month times the day equals the year.
6/10/60
Write a program that asks the user to enter a month (in numeric form), a day, and a two-digit
year. The program should then determine whether the month times the day is equal to the year.
If so, it should display a message saying the date is magic. Otherwise it should display a message
saying the date is not magic.
3. Days in a Month: Write a program that asks the user to enter the month (letting the user enter
an integer in the range of 1 through 12) and the year. The program should then display the number
of days in that month. Use the following criteria to identify leap years:
a) Determine whether the year is divisible by 100. If it is, then it is a leap year if and only if it is
divisible by 400. For example, 2000 is a leap year but 2100 is not.
b) If the year is not divisible by 100, then it is a leap year if and if only it is divisible by 4. For
example, 2008 is a leap year but 2009 is not.
Here is a sample run of the program:
Enter a month (1-12): 2
Enter a year: 2008
29 days
4. Write a function multiple that determines for a pair of integers whether the second integer is a
multiple of the first. The function should take two integer arguments and return true if the second
is a multiple of the first, false otherwise. Use this function in a program that inputs a series of
pairs of integers.
5. Long-Distance Calls: A long-distance carrier charges the following rates for telephone calls:
Write a program that asks for the starting time and the number of minutes of the call, and displays
the charges. The program should ask for the time to be entered as a floating point number in the
form HH.MM. For example, 07:00 hours will be entered as 07.00, and 16:28 hours will be entered
as 16.28.
Input Validation: The program should not accept times that are greater than 23:59. Also, no
number whose last two digits are greater than 59 should be accepted. Hint: Assuming num is a
floating-point variable, the following expression will give you its fractional part:
num − static_cast<int>(num)
6. Write a program which takes as input a number, total multiplicands and user option to get even
or odd multiplicands and print table of that number.
Example:
Input: Number=3, Total Multiplicands: 15, Multiplicands type: Even
Output:
3*2=6
3 * 4 = 12
3 * 6 = 18
3 * 8 = 24
……………………….
……………………….
3 * 14 = 42
7. Write a program to find the sum of digits of the number entered by the user. Also print it in reverse
order. For example, user enters 1234, the sum should be 10 and the program should print 4321.
(HINT: use modulus operator)
8. Distance Traveled: The distance a vehicle travels can be calculated as follows:
distance = speed * time
For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles.
Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many
hours it has traveled. The program should then use a loop to display the distance the vehicle has
traveled for each hour of that time period. Here is an example of the output:
9. Sales Bar Chart: Write a program that asks the user to enter today’s sales for five stores. The
program should then display a bar graph comparing each store’s sales. Create each bar in the bar
graph by displaying a row of asterisks. Each asterisk should represent $100 of sales. Use the
concept of loops. Here is an example of the program’s output.
10. Define a function hypotenuse that calculates the length of the hypotenuse ‘c’ of a right triangle
when the other two sides ‘a’ and ‘b’ are given. Use this function in a program using the loop to
determine the length of the hypotenuse ‘c’ for each of the following triangles. The function should
take two sides as arguments of type double and return the hypotenuse as a double.
The formula for hypotenuse calculation is as follows:
11. The Greatest and Least of These: Write a program with a loop that lets the user enter a series of
integers. The user should enter −99 to signal the end of the series. After all the numbers have
been entered, the program should display the largest and smallest numbers entered.
12. Input an integer containing only 0s and 1s (i.e., a “binary” integer) and print its decimal equivalent.
(Hint: Use the modulus and division operators to pick off the “binary” number’s digits one at a
time from right to left. Just as in the decimal number system where the rightmost digit has a
positional value of 1 and the next digit left has a positional value of 10, then 100, then 1000, etc.,
in the binary number system, the rightmost digit has a positional value of 1, the next digit left has
a positional value of 2, then 4, then 8, etc.
Thus the decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. The decimal
equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8, or 13.)