-1
#include <stdio.h>

int main(void) {
    float interest_rate, amount_of_loan, monthly_payment, remain_payment;

    printf("Enter amount of loan :");
    scanf("%f", &amount_of_loan);
  
    printf("Enter interest rate :");
    scanf("%f", &interest_rate); // this is a interest rate per a year
    printf("Monthly interest rate is : %.1f\n" , (interest_rate / 12)); // 퍼센트를 표현하는 방법 
    ((interest_rate / 12) * 0.01); //it would be 0.005

    printf("Enter monthly payment : ");
    scanf("%f", &monthly_payment);

    remain_payment = (amount_of_loan + (amount_of_loan * ((interest_rate / 12) * 0.01))) - monthly_payment;
    printf("Balance remaining after first payment : %.2f\n", remain_payment);
    // 0.005 is interest value and the payment is 386.66
  
    remain_payment = (remain_payment + (amount_of_loan * ((interest_rate / 12) * 0.01))) - monthly_payment;
    printf("Balance remaining after second payment :%.2f\n", remain_payment);

    remain_payment = (remain_payment + (amount_of_loan * ((interest_rate / 12) * 0.01))) - monthly_payment;
    printf("Balance remaining after third payment :%.2f\n", remain_payment);
    return 0;
}

Do I have to use round down function with floor function?

I searched about round up and down function in <math.h> file but I couldn't get exact result I think I know why I couldn't get the exact answer, the reason I guess was float type store my input approximately.

But I wanna get exact output that I want

chqrlie
  • 131,814
  • 10
  • 121
  • 189
jincoder
  • 21
  • 2
  • 1
    “i wanna get exact ouput that i want” is not an adequate problem description. You need to state clearly what you want. If you edited the question to provide a [mre], including sample input that reproduces the problem, an exact copy of the observed output, and a clear description of the desired output, then it might be possible to suggestion changes to the program to do what you want. – Eric Postpischil Feb 25 '23 at 14:36
  • Are you wondering why printing a value 0.005 with format %.1f results in a print of 0.0? – Lutz Lehmann Feb 25 '23 at 15:17
  • @jincoder, A simple way to "output exact value" of a FP is to use `printf("%a\n", remain_payment);`. It is _exact_, though not decimal. – chux - Reinstate Monica Feb 25 '23 at 18:41
  • "Do I have to use round down function with floor function?" --> No. You do have to explain your goals more clearly. – chux - Reinstate Monica Feb 25 '23 at 18:45
  • Tips: Instead of "퍼센트를 표현하는 방법", use "express percent" or the like. Do not use `float`. Read in data as a `double`, convert to a wide integer, perform the math, convert to `double` and write. – chux - Reinstate Monica Feb 25 '23 at 18:48
  • the problem was this " Write a program that calculates the remaining balance on a loan after the first,second, and third monthly payment and the monthly interest rate was 0.5% and monthly paymonth was 386.66 , amount of loans was 20000 the result of that answer is a first month remaining is 19713.34,a second month remaining is 19425.25, a third month remaining is 19135.71 but i couldn't get those exact answers just my answers were approximate values @Eric Postpischil – jincoder Feb 26 '23 at 06:39
  •  ./main Enter amount of loan :20000 Enter interest rate :6.0 Monthly interest rate is : 0.5 Enter monthly payment : 386.66 Balance remaining after first payment : 19713.340000 Balance remaining after second payment : 19426.680000 Balance remaining after third payment : 19140.020000 this was my result @Eric Postpischil – jincoder Feb 26 '23 at 06:58
  • Is “%a” a Alert(bell) ? What is %a? @ chux - Reinstate Monica – jincoder Feb 26 '23 at 07:56
  • @jincoder: Re “this was my result”: Yes, so? When I run the code in the question and give the input shown in your comment, I get the same output, except with only two digits after the decimal point, not six (the four trailing zeros are not present). Is that the answer you want? If so, what is the problem? If not, what answer do you want? You have to make the problem clear for people. Edit the question to show the input, an exact copy of the output obtained, and the output you want instead. Do not enter it as a comment. Edit the question to show the observed output and the desired output. – Eric Postpischil Feb 26 '23 at 12:32
  • @jincoder `%a` is a special way to print binary floating-point numbers exactly using `printf`, in base 16. Although exact, `%a` is probably not useful to you for this problem. – Steve Summit Feb 26 '23 at 13:47
  • 1
    @MikeCAT For the record, this was *not* a duplicate of [that question](https://stackoverflow.com/questions/588004). – Steve Summit Feb 26 '23 at 15:01

1 Answers1

2

You have encountered a textbook example of what's sometimes called a copy and paste error.

You first wrote

remain_payment = (amount_of_loan + (amount_of_loan * ((interest_rate / 12) * 0.01))) - monthly_payment;

which accurately computed the remaining balance after the first payment.

But then you tried to compute the second payment by writing

remain_payment = (remain_payment + (amount_of_loan * ((interest_rate / 12) * 0.01))) - monthly_payment;

But you should be computing the next remaining payment as a function of the previous running balance. The original loan amount doesn't enter into it. You changed one instance of amount_of_loan to remain_payment, but you should have changed both.

The take-home lesson here is that repeated code like this is error-prone. It's too hard to see that the almost-identical statements are the same where they're supposed to be the same, and different where they're supposed to be different.

It would be clearer and safer to use use one expression, in a loop. You can always compute the new running balance as a function of the previous one, as long as you start off with the loan amount. So I would write it like this:

remain_payment = amount_of_loan;

for(int i = 1; i <= 3; i++) {
    remain_payment = remain_payment + (remain_payment * ((interest_rate / 12) * 0.01)) - monthly_payment;
    printf("Balance remaining after payment %d: %.2f\n", i, remain_payment);
}

There are a number of other things you might want to clean up — for example, it's almost always better to use type double rather than float — but this was your main problem.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103