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.
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.