Write a Java program that computes the interest each month of a loan in accordan
ID: 671274 • Letter: W
Question
Write a Java program that computes the interest each month of a loan in accordance with the Rule of 78's. The user enters the total interest which would have been earned had the loan continued to maturity, and the number of months in the original period of the loan. The program then prints out a table, with the number of each month, the interest earned during that month by the rule, the interest earned so far, and the balance of (unearned) interest remaining at the end of that month. Do not hard code the values, and output to two decimal places. Use a method in your program. Output should look similar to below.Explanation / Answer
package com.sample;
import java.text.DecimalFormat;
import java.util.Scanner;
public class ComputeInterest {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("####0.00");
Scanner sc = new Scanner(System.in);
System.out
.println("Enter the total interest earned to loan maturity $:");
double totalInterest = Double.parseDouble(sc.next());
System.out.println("Enter theloan duration in months to maturity:");
int month = Integer.parseInt(sc.next());
System.out.println("Loan Month " + " " + " Motnth's interest" + " "
+ "Accumulated Interest" + " " + "Balance of Interest");
System.out
.println("---------------------------------------------------------------------------------------------");
System.out
.println("---------------------------------------------------------------------------------------------");
double interest = 0;
int magicNumber = 0;
sc.close();
// Getting magic number as per 78 rule
for (int count = 1; count <= month; count++) {
magicNumber += count;
}
double accumulatedInterest=0;
double balance=totalInterest;
int j=1;
for (int i = month; i >= 1; i--) {
interest = (totalInterest * i / magicNumber);
if(accumulatedInterest==0)
{
accumulatedInterest=interest;
}
else
{
accumulatedInterest+=interest;
}
balance-=interest;
System.out.println(j+++" "+df.format(interest)+" "+df.format(accumulatedInterest)+" "+df.format(balance));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.