/****************************************************************************** * This program... * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #undef NDEBUG #include "Utils.hpp" #include #include #include #include using namespace Utils; using namespace std; int main(int argc, char **argv) { if (argc != 4) { cout << fmt::format("Syntax: {0:s} loan_amount rate_per_annum term_in_years", argv[0]) << endl; exit(1); } long balance = Utils::stolWithDefault(string(argv[1]), 0) * 100; double interest_rate = Utils::stodWithDefault(string(argv[2]), 0.05); interest_rate /= 12.0; int periods = Utils::stoiWithDefault(string(argv[3]), 30); periods *= 12; long payment(interest_rate * balance / (1.0 - pow(1.0 + interest_rate, -periods)) + 0.5); cout << "Month\tPayment\tPrinciple\tInterest\tBalance" << endl; cout << fmt::format("0\t\t\t\t{0:.2f}", balance / 100.0) << endl; for (int i = 0; i < periods; ++i) { long const INTEREST = long(balance * interest_rate + 0.5); long principle = payment - INTEREST; balance -= principle; if (i == periods - 1) { payment += balance; principle += balance; balance = 0; } cout << fmt::format("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, payment / 100.0, principle / 100.0, INTEREST / 100.0, balance / 100.0) << endl; } return 0; }