From 8be85586035e29c01e9f1f7598a5d1a1752a1949 Mon Sep 17 00:00:00 2001 From: Surepic <46145992+ioprojecton@users.noreply.github.com> Date: Wed, 7 Jul 2021 07:23:10 -0400 Subject: [PATCH] Additional check for not going to negative balance in original implementation there is not check for the remaining balance. --- 06/projects/09/9.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/06/projects/09/9.c b/06/projects/09/9.c index 1cc60d9c..47142b40 100644 --- a/06/projects/09/9.c +++ b/06/projects/09/9.c @@ -5,9 +5,9 @@ int main(void) { float loan = 0.0f, rate = 0.0f, payment = 0.0f; - int i, - num_of_payments; + int numberOfPayments = 0; + printf("Enter amount of loan: "); scanf("%f", &loan); @@ -18,12 +18,22 @@ int main(void) { scanf("%f", &payment); printf("Enter number of payments: "); - scanf("%d", &num_of_payments); + scanf("%d",&numberOfPayments); - for (i = 1; i <= num_of_payments; i++) { + int counter = 1; + + while (counter <= numberOfPayments) { + loan = loan - payment + (loan * rate / 100.0 / 12.0); - printf("Balance remaining after payment %d: $%.2f\n", i, loan); + + if (loan > 0) printf("Balance remaining after %d payment: $%.2f\n",counter, loan); + + else break; + + counter++; + } + return 0; }