Hi,
I am new to C# programming and my first assignment requires me to calculate total interest paid on a loan. The user will input the loan, the interest rate per month, and the monthly payment. It has to use a while loop and calculate the following:
Total amount of interest paid and the number of months to pay off the loan.
I am not a finance major so I have some difficulty with the logistics of how to go about doing this. My code so far is really messy and does not work. Please help!
I am new to C# programming and my first assignment requires me to calculate total interest paid on a loan. The user will input the loan, the interest rate per month, and the monthly payment. It has to use a while loop and calculate the following:
Total amount of interest paid and the number of months to pay off the loan.
I am not a finance major so I have some difficulty with the logistics of how to go about doing this. My code so far is really messy and does not work. Please help!
Code:
static void Main(string[] args) { //declare variables double amount_borrowed; double interest_rate; double monthly_payment; double leftover_balance; double monthly_interest_payment; double adjusted_principle; double balance; double total_interest_paid; double total_months; int monthCounter; //initialize variables amount_borrowed = 0; interest_rate = 0; monthly_payment = 0; adjusted_principle = 0; monthly_interest_payment = 0; monthCounter = 0; total_months = 0; leftover_balance = 0; total_interest_paid = 0; balance = 0; adjusted_principle = amount_borrowed - monthly_interest_payment; monthly_interest_payment = adjusted_principle * interest_rate; leftover_balance = monthly_payment - monthly_interest_payment; monthly_interest_payment = (amount_borrowed * interest_rate); balance = adjusted_principle - leftover_balance; total_interest_paid = monthly_interest_payment; //prompt for input Console.WriteLine("Enter amount borrowed: "); amount_borrowed = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter monthly interest rate: "); interest_rate = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter monthly payment: "); monthly_payment = Convert.ToDouble(Console.ReadLine()); //loop until balance is 0 while (balance > 0) { //Count the number of months monthCounter++; //Calculate the balance balance = adjusted_principle - leftover_balance; // Calculate the total interest paid total_interest_paid = monthly_interest_payment * total_months; Console.WriteLine("Your new balance: " + balance); balance = Convert.ToDouble(Console.ReadLine()); }//end while Console.WriteLine("You paid " + total_interest_paid + " in interest"); total_interest_paid = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("It took " + total_months + " months to pay off your loan"); total_months = Convert.ToDouble(Console.ReadLine()); } } }
Comment