0% found this document useful (0 votes)
29 views4 pages

Assg 2

Uploaded by

Nick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Assg 2

Uploaded by

Nick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSCI1540 Fundamental Computing with C++, Fall 2024/25

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Assignment 2: Mortgage Calculator


Due: 23:59, Wed 2 Oct 2024 File name: mortgage.cpp Full marks: 100

Introduction
The objective of this assignment is to practice the use of variables, expressions, and console
input/output. You will write a program to perform mortgage calculations.

Most people buying a real property (房地產) need to borrow money from a financial institution with
a mortgage (按揭). When a property is priced at $𝑝, a buyer applying a mortgage loan amount $𝑙 is
borrowing at a loan ratio (按揭成數) 𝜎 = 𝑝𝑙 and would need a down payment (首期) $𝑑 = 𝑝 − 𝑙.
(Usually 𝜎 cannot be too high, e.g., at most 90%.) The loan is at a yearly interest rate (年利率) 𝑟𝑦
𝑟
(that is, monthly interest rate is 𝑟𝑚 = 12𝑦) and is paid back monthly over 𝑦 years. That is, a total of
𝑛 = 𝑦 × 12 monthly repayments are needed. For simplicity, in this assignment we assume that 𝑟𝑦
and 𝑟𝑚 remain unchanged throughout the mortgage period. The monthly payment amount $𝑐 can
be calculated as:

𝑟𝑚 × 𝑙 × (1 + 𝑟𝑚 )𝑛
𝑐=
(1 + 𝑟𝑚 )𝑛 − 1

Example 1: a $2500000 property with loan amount $2000000 and yearly interest rate 6.5% for 30
years. (𝑝 = 2500000, 𝑙 = 2000000, 𝑟𝑦 = 0.065, 𝑦 = 30)

2000000
Loan ratio 𝜎 = 2500000 = 80%
Down payment 𝑑 = 2500000 − 2000000 = 500000
0.065
Monthly interest rate 𝑟𝑚 = 12
Number of repayments 𝑛 = 30 × 12 = 360
0.065 0.065 360
12 ×2000000×(1+ 12 )
Monthly payment 𝑐 = 360 = 12641.36 (two decimal places)
(1+0.065
12 ) −1

Example 2: a $8000000 property with loan amount $6800000 and yearly interest rate 4.125% for 25
years. (𝑝 = 8000000, 𝑙 = 6800000, 𝑟𝑦 = 0.04125, 𝑦 = 25)

6800000
Loan ratio 𝜎 = = 85%
8000000
Down payment 𝑑 = 8000000 − 6800000 = 1200000
0.04125
Monthly interest rate 𝑟𝑚 = = 0.0034375
12
Number of repayments 𝑛 = 25 × 12 = 300
0.0034375×6800000×(1+0.0034375)300
Monthly payment 𝑐 = (1+0.0034375)300 −1
= 36363.90 (two decimal places)

Besides the monthly payment amount 𝑐, we can also calculate how much the borrower still owes
after 𝑘 monthly payments are made:

(1 + 𝑟𝑚 )𝑘 − 1
Amount still owed after 𝑘 months = 𝑙 × (1 + 𝑟𝑚 )𝑘 − 𝑐 ×
𝑟𝑚

Copyright © 2024 CSE, CUHK Page 1 of 4


CSCI1540 Fundamental Computing with C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong

Furthermore, we can calculate the total interest paid after 𝑘 monthly payments are made:

(1 + 𝑟𝑚 )𝑘 − 1
Total interest paid after 𝑘 months = (𝑙 × 𝑟𝑚 − 𝑐) × +𝑐×𝑘
𝑟𝑚

Using Example 1, we can calculate that after, say, 20 months, amount still owed is $1961916.80 and
total interest paid is $214744.01. Similarly, using Example 2, after 20 months, amount still owed is
$6531561.16 and total interest paid is $458839.07.

Program Specification
1. At first, the program shall obtain four user inputs: (a) property price 𝑝, (b) loan amount 𝑙, (c)
yearly interest rate 𝑟𝑦 , and (d) number of years 𝑦 of the mortgage.
2. The above inputs are valid only if all the following conditions are met:
• property price is positive; and
• loan amount is positive but at most 90% of the property price; and
• yearly interest rate is positive; and
• number of years is positive.

In case any of the inputs is invalid, the program shall print a warning message and then
terminate.

3. Otherwise, the loan ratio 𝜎 (in percentage), down payment 𝑑, and monthly payment 𝑐 shall be
computed and displayed.
4. Next, the program shall obtain one more user input: (e) the number of monthly payments made
𝑘.
5. This input is valid only if it is at least 0 but smaller than 𝑛. In case this input is invalid, the
program shall print a warning message and then terminate.
6. Otherwise, the amount still owed and total interest paid after 𝑘 months shall be determined and
displayed.

You can assume that user inputs (a)–(c) are always entered as floating point numbers and user
inputs (d)-(e) are always entered as integers. Therefore, you can use the double data type for
storing (a)-(c) and the int data type for storing (d)-(e).

When doing power calculations (𝑎 𝑏 ), you can use the C++ function pow(…, …). You must
#include <cmath> at the top of your program before using it.

When printing the loan ratio, down payment, monthly payment, amount still owed, and total
interest paid, the numbers shall be printed in fixed point notation (never scientific notation) and two
decimal places. This can be done by:

cout << … << fixed << setprecision(2) << number_to_be_printed;

To use the above, you must #include <iomanip> at the top of your program.

Copyright © 2024 CSE, CUHK Page 2 of 4


CSCI1540 Fundamental Computing with C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong

Sample Runs
In the following sample runs, the blue numbers after ‘?’ are user inputs and the other text is the
program printout. You can try the provided sample program for other inputs. Your program printout
shall be exactly the same as the sample program (same text, symbols, letter case, spacings, etc.).
Note that there is a space after the ‘?’ included in each input prompt.

Property price? 2500000↵


Loan amount? 2000000↵
Yearly interest rate? 0.065↵
Mortgage years? 30↵
Loan ratio: 80.00%
Down payment: $500000.00
Monthly payment: $12641.36
How many months repaid? 20↵
Amount still owed after 20 month(s): $1961916.80
Total interest paid after 20 month(s): $214744.01

Property price? 8000000↵


Loan amount? 6800000↵
Yearly interest rate? 0.04125↵
Mortgage years? 25↵
Loan ratio: 85.00%
Down payment: $1200000.00
Monthly payment: $36363.90
How many months repaid? 20↵
Amount still owed after 20 month(s): $6531561.16
Total interest paid after 20 month(s): $458839.07

Property price? 15600000↵


Loan amount? 15000000↵
Yearly interest rate? 0.025↵
Mortgage years? 15↵
Invalid input!

Property price? 15600000↵


Loan amount? 8000000↵
Yearly interest rate? 0.025↵
Mortgage years? 15↵
Loan ratio: 51.28%
Down payment: $7600000.00
Monthly payment: $53343.14
How many months repaid? 500↵
Invalid input!

Copyright © 2024 CSE, CUHK Page 3 of 4


CSCI1540 Fundamental Computing with C++, Fall 2024/25
Department of Computer Science and Engineering, The Chinese University of Hong Kong

Submission and Marking


▪ Your program file name shall be mortgage.cpp. Submit the file in Blackboard
(https://blackboard.cuhk.edu.hk/).
▪ Insert your name, student ID, and e-mail as comments at the beginning of your source file.
▪ You can submit your assignment multiple times. Only the latest submission counts.
▪ Your program shall be free of compilation errors and warnings when built in VS Community 2022.
▪ Your program shall include suitable comments as documentation.
▪ Do NOT share your work to others and do NOT plagiarize. Both senders and plagiarists shall be
penalized.

Copyright © 2024 CSE, CUHK Page 4 of 4

You might also like