Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Practice Program help

Name: Anonymous 2011-02-15 22:21

So, I've messed around with this little practice program (I'm somewhat of a beginner programmer), but can't seem to yield the exact output that I should be getting.

Here is what is asked for:
Write a program that calculates the remaining balance on a loan after the first, second, and third monthly payments.

Here is the sample I/O, as well as my results:
Enter amount of loan: 2000.00
Enter interest rate: 6.0
Enter monthly payment: 386.66

Balance after first payment : 19713.34 (mine gives 19711.41)
Balance after second payment: 19425.25 (mine gives 19425.25)
Balance after third payment : 19135.71 (mine gives 19129.88)

Here is the code I have written:
#include <stdio.h>

int main(void)
{
  float loan, rate, monthpay;
 
  printf("Enter loan amount: $");
  scanf("%f", &loan);
  printf("Enter interest rate: ");
  scanf("%f", &rate);
  rate = rate/100.00/12.00 + 1.00;
  printf("Enter monthly payment: $");
  scanf("%f", &monthpay);
  printf("The current rate is: %f\n", rate);
  printf("Balance remaining after first payment: $%.2f\nBalance remaining after second payment: $%.2f\nBalance remaining after third payment: $%.2f\n", (loan - monthpay) * rate, (((loan - monthpay) * rate) - monthpay) * rate, (((((loan - monthpay) * rate) - monthpay) * rate) - monthpay) * rate);
  return 0;
}

What this code should be doing is finding the monthly interest rate (I think this is done by dividing the rate by 100, then dividing it by 12), then subtracting the monthly payment from the amount of the loan, and then adding interest to that amount (by multiplying by the rate+1).
I am pretty sure I did most of this wrong, my hunch is that it has something to do with me not using the float properly or something, since the answers are off by such a small amount.

Name: Anonymous 2011-02-15 22:22

did most of this right*, not wrong. I just think that a few decimals are being rounded somewhere, I'm not sure where though.

Name: Anonymous 2011-02-15 22:33

>not using doubles
>using print statements to do complicated calculations

Name: Anonymous 2011-02-15 22:35

>>3
okay, well where I am in the book it hasn't taught anything about using doubles yet, so is it possibly something else? Doesn't seem likely that I'd be given a project to do without being taught what is required to complete it. Also I was using the print statement just as practice, I can repost the other copy if it really matters.

Name: Anonymous 2011-02-15 22:38

That last line before the return looks like shit.
Unreadable code can and will result in undesirable execution.
In the future, use either [code] tags or pastebin.

Name: Anonymous 2011-02-15 22:39

This is the other way I wrote it but it obviously gives the same results...

#include <stdio.h>

int main(void)
{
  float loan, rate, monthpay, mon1, mon2, mon3;
 
  printf("Enter loan amount: $");
  scanf("%f", &loan);
  printf("Enter interest rate: ");
  scanf("%f", &rate);
  rate = rate/100.00/12.00 + 1.00;
  printf("Enter monthly payment: $");
  scanf("%f", &monthpay);
  mon1 = (loan - monthpay) * rate;
  loan = mon1;
  mon2 = (loan - monthpay) * rate;
  loan = mon2;
  mon3 = (loan - monthpay) * rate;
  printf("The current rate is: %f\n", rate);
  printf("Balance remaining after first payment: $%.2f\nBalance remaining after second payment: $%.2f\nBalance remaining after third payment: $%.2f\n", mon1, mon2, mon3);
  return 0;
}

Name: Anonymous 2011-02-15 22:39

>>5
oh okay sorry, will do from now on

Name: Anonymous 2011-02-15 22:42

It's bad practice to initialize variables without giving them a value.

Name: Anonymous 2011-02-15 22:47

>>8
okay, I'll try giving them the initial value of 0 until they receive different values, would that help?

Name: Anonymous 2011-02-15 22:54

initializing the variables in the same step as the declaration did nothing, same results. Any ideas why my answers are off (by such a small amount too :S)?
I'm thinking it's either something to do with the rate conversion, or the rounding of values throughout the code, the only problem is I've looked this over about 100 times now and still don't see the problem.

Name: Anonymous 2011-02-15 23:06

Nevermind I finally managed to figure it out.
In case any of you are curious, the problem was in the "mon" calculations, where it says (loan - monthpay) * rate, it was actually supposed to be loan * rate - monthpay.
Thanks anyways to those of you who tried to help me :)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List