/****************************************************************************** * This program computes a loan amortization table. * * 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); } double balance = Utils::stodWithDefault(string(argv[1]), 0.0); double const INTEREST_RATE = Utils::stodWithDefault(string(argv[2]), 0.05) / 12.0; int const PERIODS = Utils::stoiWithDefault(string(argv[3]), 30) * 12; double const PAYMENT = INTEREST_RATE * balance / (1.0 - pow(1.0 + INTEREST_RATE, -PERIODS)); cout << "Month\tPayment\tPrinciple\tInterest\tBalance" << endl; cout << fmt::format("0\t\t\t\t{0:.2f}", balance) << endl; for (int i = 0; i < PERIODS; ++i) { double const INTEREST = balance * INTEREST_RATE; double const PRINCIPLE = PAYMENT - INTEREST; balance -= PRINCIPLE; cout << fmt::format("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, PAYMENT, PRINCIPLE, INTEREST, balance) << endl; } return 0; }