4003-231: CS1 Homework Programs #1 Due: Thur. week 4 ASSIGNMENT & EXPRESSIONS ======================== Grading Policy ================================ Each question below has a number of points associated with it, you are required to turn in 3 points worth of problems. ===================== Things to Turn in ================================ You should turn in a printout of your program (make sure you name and the homework number is at the top. In addition, you should turn in a printout of the running of your program. To get a file that contains the output of everything that is printed on the screen (in Unix) you can use the "script" command. For information on how to use the command type man script on the Unix command line. When providing output from your program you should run your program multiple times, with a couple of the runs using data not provided in the examples. ===================== Homework Programs #1 ============================= 1. (1 pt) Write a program which asks the user to enter a double and then prints out the number truncated to two decimal places. For example: 32.4851 would truncated to 32.48 72.9431 would truncated to 72.94 54.0000 would truncated to 54.00 4.9999 would truncated to 4.99 Your program should just use type conversions and arithmetic to round the number. For example: 32.4851 is the number I want to truncate. 32.4851 * 100 gives 3248.51 with 3248.51 if I drop the fractional part I get 3248 3248 / 100 gives 32.48, which is the result. 2. (1 pt) Modify the program you wrote in problem 1 above to round the number instead of truncating it. For example: 32.4851 would rounded to 32.49 72.9431 would rounded to 72.94 54.0000 would rounded to 54.00 4.9999 would rounded to 5.00 3. (2 pts) Write a program that computes the monthly payment of an amortized loan (such as a mortgage or student loan) and the total interest paid during the life of the loan. The program should ask for the following information from the user: 1. The amount of the loan 2. The yearly interest rate, such as 7.75% 3. The term of the loan in years The program should then calculate the payment of the loan and the total interest for the full term and print out these values. The formula for the monthly payment of an amortized loan is the following: int Pmt = Amt * ------------------- -n 1 - ( 1 + int ) where: Pmt The monthly payment Amt Amount of the loan int Interest for one month n Term of the loan in months Note: the bottom of the equation is 1 minus the quantity (1 + int) raised to the negative n'th power. To calculate the total interest paid for a full term loan, use the formula: Pmt * n - Amt Input ----- The program must ask for the amount of the loan, the interest rate as a percentage and the term in years. Note the differences between the units of the input items and those used in the formula. Output ------ The program should print out a sign-on message stating what it does, prompt the user for the required values and print the monthly payment and total interest. When you run the program, the output should look like: Loan amortization program Enter the loan amount: 25000 Enter the interest rate (like 7.25): 8.25 Enter loan term in years: 5 Loan amount = $25000.00, at 8.25% for 5 years Monthly payment = $509.9062916328536 Total interest = $5594.377497971214 Another example run looks like this: Loan amortization program Enter the loan amount: 100001 Enter the interest rate (like 7.25): 4.975 Enter loan term in years: 30 Loan amount = $100001.0, at 4.975% for 30 years Monthly payment = $535.3001220611827 Total interest = $92707.04394202578 Notes ----- To raise x to the power of y, you can use the "pow" method in the standard Math class. Calling z = Math.pow( x, y ) raises x to the y power and saves the result in the variable z. 4. (3 pts) Modify the loan program in problem 3 above to round the numbers to two decimal places prior to printing them out. Note you want to round the numbers only at the last possible moment to minimize rounding error.