Concept
How can a loan amortization schedule handle rounded payments in C?
ComputerScienceOne / Paying the Piper
"Let’s adapt the solution for the loan amortization schedule we developed in Section 4.7.3. First, we’ll read the principle, terms, and interest as command line inputs. Adapting the formula for the monthly payment and using the standard math library’s pow() function, we get:\n\ndouble monthlyPayment = (monthlyInterestRate * principle) / 2 (1 - pow( (1 + monthlyInterestRate), -n));\n\nHowever, recall that we may have problems due to accuracy. The monthly payment could come out to be a fraction of a cent, say $43.871. For accuracy, we need to ensure that all of the figures for currency are rounded to the nearest cent. The standard math library does have a round() function, but it only rounds to the nearest whole number, not the nearest 100th. However, we can adapt the “off-the-shelf” solution to fit our needs. If we take the number, multiply it by 100, we get (say) 4387.1 which we can now round to the nearest whole number, giving us 4387. We can then divide by 100 to get a number that has been rounded to the nearest 100th! In C, we could simply do the following.\n\nmonthlyPayment = round(monthlyPayment * 100.0) / 100.0;\n\nWe can use the same trick to round the monthly interest payment and any other number expected to be whole cents. To output our numbers, we use printf() and take care to align our columns to make it look nice. To finish our adaptation, we handle the final month separately to account for an over/under payment due to rounding. The full solution can be found in Code Sample 17.8.\n\n#include <stdio.h>\n\n#include <stdlib.h>\n\n#include <math.h>\n\nint main(int argc, char **argv) {\n\nif(argc != 4) {\n\nprintf(\"Usage: %s principle apr terms\n\n\", argv[0]);\n\nexit(1);\n\n}\n\ndouble principle = atof(argv[1]);\n\ndouble apr = atof(argv[2]);\n\nint n = atoi(argv[3]);\n\ndouble balance = principle;\n\ndouble monthlyInterestRate = apr / 12.0;\n\nint i;\n\n//monthly payment\n\ndouble monthlyPayment = (monthlyInterestRate * principle) /\n\n(1 - pow( (1 + monthlyInterestRate), -n));\n\n//round to the nearest cent\n\nmonthlyPayment = round(monthlyPayment * 100.0) / 100.0;\n\nprintf(\"Principle: $%.2f\n\n\", principle);\n\nprintf(\"APR: %.4f%%\n\n\", apr*100.0);\n\nprintf(\"Months: %d\n\n\", n);\n\nprintf(\"Monthly Payment: $%.2f\n\n\", monthlyPayment);\n\n//for the first n-1 payments in a loop:\n\nfor(i=1; i<n; i++) {\n\n// compute the monthly interest, rounded:\n\ndouble monthlyInterest =\n\nround( (balance * monthlyInterestRate) * 100.0) / 100.0;\n\n// compute the monthly principle payment\n\ndouble monthlyPrinciplePayment = monthlyPayment - monthlyInterest;\n\n// update the balance\n\nbalance = balance - monthlyPrinciplePayment;\n\n// print i, monthly interest, monthly principle, new balance\n\nprintf(\"%d\\t$%10.2f $%10.2f $%10.2f\n\n\", i, monthlyInterest,\n\nmonthlyPrinciplePayment, balance);\n\n}\n\n//handle the last month and last payment separately\n\ndouble lastInterest = round( (balance * monthlyInterestRate) * 100.0) / 100.0;\n\ndouble lastPayment = balance + lastInterest;\n\nprintf(\"Last payment = $%.2f\n\n\", lastPayment);\n\nreturn 0;\n\n}\n\nCode Sample 17.8.: Loan Amortization Program in C"
Related Ideas
- How can a PHP program calculate and round a monthly loan payment?ComputerScienceOne · Paying the Piper
- How can a loan amortization schedule be adapted in C?ComputerScienceOne · Nested Loops
- What is 28?ComputerScienceOne · Paying the Piper
- What is ", i, monthlyInterest, 39 monthlyPrinciplePayment, balance); 40 } 41 42 //handle the last month and last payment separately 43 double lastInterest = Math?ComputerScienceOne · Paying the Piper
- How can a generalized rounding method round numbers to any decimal place?ComputerScienceOne · Generalized Rounding
- How can a generalized rounding function round values to different decimal places?ComputerScienceOne · Generalized Rounding
- How do default parameter values work in PHP functions?ComputerScienceOne · Optional & Default Parameters
- How do C functions return values to the calling function?ComputerScienceOne · Void Functions