-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

11
  • 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 minimal reproducible example, 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. Commented Feb 25, 2023 at 14:36
  • Are you wondering why printing a value 0.005 with format %.1f results in a print of 0.0? Commented Feb 25, 2023 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. Commented Feb 25, 2023 at 18:41
  • "Do I have to use round down function with floor function?" --> No. You do have to explain your goals more clearly. Commented Feb 25, 2023 at 18:45
  • 1
    @MikeCAT For the record, this was not a duplicate of that question. Commented Feb 26, 2023 at 15:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.