Avoiding Financial Nightmare
InterviewTime limit1sMemory limit128 MB
Given a loan principal, a term in months, and a monthly interest rate, find the fixed monthly payment so the balance hits zero at term end.
- Level
Medium5 of 10
- Topics
- Binary search, Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
Nowadays, taking out a loan from a bank or a financial company has become very common, whether for business or personal purposes. If you manage your spending well, a loan or a credit card can be a great help; otherwise, it can become your worst financial nightmare. A professor has decided to take out a loan to buy a new house in a pleasant city in Indonesia.
Three values affect his monthly bill:
- Principal : the remaining amount of the loan
- Period : the number of months needed to pay off the loan
- Rate : the monthly interest rate (a percentage)
Banks and financial companies usually offer a fixed-payment plan. Every month the customer pays the same amount (the total monthly payment), which is made up of two parts. Let be the remaining principal in a given month.
The interest payment is the rate applied to the remaining principal, rounded up to the nearest integer.
The principal payment is the total monthly payment minus the interest payment, and the remaining principal decreases by that amount.
The total monthly payment must be the same every month, and it must be chosen so that when the period ends the remaining principal is 0 — or, if reaching exactly 0 is impossible, the negative amount nearest to 0. Find that total monthly payment .
For example, with a principal of $42,000, a monthly interest rate of 5%, and a 5-month term, the monthly payment is $9,701.
- Term 1: interest payment = ⌈5% × $42,000⌉ = $2,100, principal payment = $9,701 − $2,100 = $7,601, remaining principal = $42,000 − $7,601 = $34,399
- Term 2: interest payment = ⌈5% × $34,399⌉ = $1,720, principal payment = $9,701 − $1,720 = $7,981, remaining principal = $34,399 − $7,981 = $26,418
After the 5th term the remaining principal is exactly $0.
Input
The input consists of several test cases. Each case is given as three integers , , and .
- (): the initial principal
- (): the period in months
- (): the monthly interest rate as a percentage
Process every case until the end of input.
Output
For each case, print the total monthly payment that satisfies the condition on its own line. The value is unique: it is the smallest fixed monthly payment that makes the remaining principal 0, or the negative amount nearest to 0 if 0 is impossible, when the period ends.