Question: C# Calc loan interest total Needs: Loantable function to display an amortization schedule that include: number of the cycle, payment amount, principal paid, interest paid,
C# Calc loan interest total
Needs: Loantable function to display an amortization schedule that include:
number of the cycle, payment amount, principal paid, interest paid, total interest paid, balance
Everything else works, but the TotalInterestPaid is not calculating and showing a running total. (this is the seperate loan class)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Loan_Program
{
class Loan
{
//declare variables
private double LoanAmount, InterestRate;
private int LoanLength;
//Constructor of Loan class that takes amount, rate and years
public Loan(double amount, double rate, int years)
{
this.LoanAmount = amount;
this.InterestRate = (rate / 100.0) / 12.0;
this.LoanLength = years;
}
//returns the monnthly payment
public double GetMonthlyPayment()
{
int months = LoanLength * 12;
return (LoanAmount * InterestRate * Math.Pow(1 + InterestRate, months)) / (Math.Pow(1 + InterestRate, months) - 1);
}
//Returns the total intereest paid
public double TotalInterestPaid()
{
double interest = LoanAmount * InterestRate;
double total = 0;
for (int month = 1; month <= LoanLength * 12; month++)
total += interest;
return total;
}
//prints the amortization of Loan
public void LoanTable()
{
double monthlyPayment = GetMonthlyPayment();
double principalPaid = 0;
double newBalance = 0;
double interestPaid = 0;
double principal = LoanAmount; ;
double totalinterest = TotalInterestPaid();
//nonth, payment amount, principal paid, interest paid, total interest paid, balance
Console.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}{4,-10}{5,-10}", "Month", "payment", "Interest Paid", "Principal paid","Balance","Total Interest");
for (int month = 1; month <= LoanLength * 12; month++)
{
// Compute amount paid and new balance for each payment period
interestPaid = principal * InterestRate;
principalPaid = monthlyPayment - interestPaid;
newBalance = principal - principalPaid;
totalinterest = TotalInterestPaid(interestPaid);
// Output the data item
Console.WriteLine("{0,-7}{1,10:N2}{2,10:N2}{3,10:N2}{4,15:N2}{5,15:N2}",
month, monthlyPayment, interestPaid, principalPaid, newBalance, totalinterest);
// Update the balance
principal = newBalance;
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
