You never subtract the payment from the loan. Therefore the outstanding loan grows forever.
EDIT:
Here's working code:
Code:
#include <stdio.h>
int main()
{
double loan_amt;
double interest_rate;
double remaining;
double interest;
double payment;
int month = 1;
printf("Enter the loan amount\n");
scanf("%lf", &loan_amt);
printf("Enter the monthly interest rate of the loan\n");
scanf("%lf", &interest_rate);
printf("Enter the amount of the monthly payment\n");
scanf("%lf", &payment);
printf("+-------+---------+-----------+----------+\n");
printf("| Month | Payment | Interest | Remaining | \n");
printf("+-------+---------+-----------+----------+\n");
interest_rate = (loan_amt*(interest_rate/100));
while(loan_amt > 0)
{
interest = (loan_amt*(interest_rate/100));
/* EDIT START EDIT START EDIT START */
/* notice the "-payment" at the end, that's the only part i added */
loan_amt = loan_amt+interest-payment;
/* EDIT END EDIT END EDIT END */
printf("| %3d | %5.2lf | %4.2lf | %.2lf |\n",
month, payment, interest, loan_amt);
month++;
if(payment > (loan_amt + interest))
{
interest = (loan_amt*(interest_rate/100));
payment = (loan_amt + interest);
loan_amt = 0;
printf("| %3d | %5.2lf | %4.2lf | %.2lf |\n",
month, payment, interest, loan_amt);
}
}
system("PAUSE");
return 0;
}
EDIT #2:
You also need to add code to check if the interest is greater than the payment. If that is the case the loop just goes on forever...