C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
amort.c File Reference

Installment Loan Amortization. More...

#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
Include dependency graph for amort.c:

Go to the source code of this file.

Macros

#define _XOPEN_SOURCE

Functions

int main (int argc, char **argv)
void print_totals (Amort *)

Variables

char month [12][4]

Detailed Description

Installment Loan Amortization.

Author
Bill Waller Copyright (c) 2026 MIT License billx.nosp@m.wall.nosp@m.er@gm.nosp@m.ail..nosp@m.com
Date
2026-05-05

Definition in file amort.c.

Macro Definition Documentation

◆ _XOPEN_SOURCE

#define _XOPEN_SOURCE

Definition at line 10 of file amort.c.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )
Examples
/usr/local/src/C-Menu/src/enterchr.c, and /usr/local/src/C-Menu/src/enterstr.c.

Definition at line 34 of file amort.c.

34 {
35 setlocale(LC_NUMERIC, "");
36 if (argc > 1 &&
37 ((strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) ||
38 argc < 5)) {
39 printf("Usage: amort [present_value][number_of_payments]"
40 "[interest_rate][payment_amount][yyyy-mm-dd]\n\n");
41 exit(EXIT_SUCCESS);
42 }
43 struct tm tm;
44 Amort *amort = malloc(sizeof(Amort));
45 sscanf(argv[1], "%lf", &amort->pv);
46 sscanf(argv[2], "%lf", &amort->n);
47 sscanf(argv[3], "%lf", &amort->i);
48 sscanf(argv[4], "%lf", &amort->pmt);
49 if (amort->pv == 0 || amort->n == 0 || amort->i == 0 || amort->pmt == 0 ||
50 argv[5][0] == '\0') {
51 fprintf(stderr, "Error: All arguments must be non-zero.\n");
52 exit(EXIT_FAILURE);
53 }
54 amort->interest = amort->pv * amort->i / 1200;
55 if (amort->interest > amort->pmt) {
56 fprintf(stderr, "Error: Payment amount less than interest\n");
57 exit(EXIT_FAILURE);
58 }
59 memset(&tm, 0, sizeof(tm));
60 if (strptime(argv[5], "%Y-%m-%d", &tm) == NULL)
61 if (strptime(argv[5], "%Y%m%d", &tm) == NULL)
62 printf("Error: Invalid date format. Use yyyy-mm-dd or yyyymmdd.\n"),
63 exit(EXIT_FAILURE);
64 amort->year_total_interest = 0;
65
66 printf("First Payment: %04d-%02d-%02d\n\n", tm.tm_year + 1900,
67 tm.tm_mon + 1, tm.tm_mday);
68 printf(" Per Mth Year Balance Payment Principal Interest\n");
69 printf("---- --- ---- ------------ ---------- ---------- ----------\n");
70 int m = 0;
71 for (int x = 0; x < amort->n; x++) {
72 if (amort->pv <= 0)
73 break;
74 m = (x + tm.tm_mon) % 12;
75 int y = ((x + tm.tm_mon) / 12) + tm.tm_year + 1900;
76 amort->interest = amort->pv * amort->i / 1200;
77 amort->year_total_interest += amort->interest;
78 if (amort->pv + amort->interest < amort->pmt) {
79 amort->pmt = amort->pv + amort->interest;
80 amort->principal = amort->pv;
81 } else
82 amort->principal = amort->pmt - amort->interest;
83 amort->pv -= amort->principal;
84 printf("%4d %4s %4d %'12.2f %'10.2f %'10.2f %'10.2f\n", x + 1, month[m],
85 y, amort->pv, amort->pmt, amort->principal, amort->interest);
86 if (m == 11)
87 print_totals(amort);
88 }
89 if (m != 11)
90 print_totals(amort);
91 free(amort);
92 return EXIT_SUCCESS;
93}
void print_totals(Amort *)
Definition amort.c:94
char month[12][4]
Definition amort.c:19

References month, and print_totals().

Here is the call graph for this function:

◆ print_totals()

void print_totals ( Amort * amort)

Definition at line 94 of file amort.c.

94 {
95 printf(" "
96 "----------\n");
97 printf(" %'10.2f\n",
98 amort->year_total_interest);
99 printf(" "
100 "==========\n\n");
101 amort->year_total_interest = 0;
102}

Referenced by main().

Here is the caller graph for this function:

Variable Documentation

◆ month

char month[12][4]
Initial value:
= {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

Definition at line 19 of file amort.c.

19 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
20 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

Referenced by main().