Thursday, April 4, 2013

Amortization program

When you borrow money from a bank, you may want to track some information that relate to your payment to the bank. These information can be old balance , monthly payment, interest paid, principle paid, and new balance from the start month to the end of borrowing period. The AmortizationProgram here can assist you to obtain these information. The program allows the user to input the amount of load, interest rate per year, and number of years. Then the program displays an amortization schedule that includes the information as mentioned above. The formula used to calculate the monthly payment is shown below.
monthly payment formula used in Java amortization program

AmortizationProgram source code:

1 import java.util.Scanner;
2 class AmortizationProgram{
 
3   public static void main(String[] args){
4    double p,iy;
5      int n;
6      Scanner sc=new Scanner(System.in);
7    System.out.print("Enter amount of loan:");
8      p=sc.nextFloat();
9      System.out.print("Enter interest rate per year:");
10    iy=sc.nextFloat();
11    System.out.print("Enter number of years:");
12    n=sc.nextInt();    
13    calAmort(p,iy,n);
     
14    }

15     public static void calAmort(double p,double iy, int ny){
16          double newbal;
17          double im=(iy/12)/100;
18          int nm=ny*12;
19           double mp,ip,pp;
20           int i;
   
21     mp=p*im*Math.pow(1+im,(double)nm)/(Math.pow(1+im,(double)nm)-1);
22     printHeader();
23 //print amortization schedule for all months except the last month
24     for(i=1;i<nm;i++){    
25       ip=p*im;//interest paid
26       pp=mp-ip; //princial paid
27       newbal=p-pp; //new balance                
28         printSch(i,p,mp,ip,pp,newbal);
29       p=newbal;  //update old balance
30        }
31       //last month
32       pp=p;
33       ip=p*im;
34        mp=pp+ip;
35        newbal=0.0;
36        printSch(i,p,mp,ip,pp,newbal);      
   
37    }
 
38 public static void printSch(int i,double p,double mp,double ip,double pp,double newbal){
   
39   System.out.format("%-8d%-12.3f%-10.3f%-10.3f%-10.3f%-12.3f\n",i,p,mp,ip,pp,newbal);
   
40    }

41 public static void printHeader(){
42       int i;
43     System.out.println("\nAmortization Schedule for  Borrower");
44     for(i=0;i<62;i++)  System.out.print("-");
45       System.out.format("\n%-8s%-12s%-10s%-10s%-10s%-12s"," ","Old","Monthly","Interest","Principle","New","Balance");
46       System.out.format("\n%-8s%-12s%-10s%-10s%-10s%-12s\n\n","Month","Balance","Payment","Paid","Paid","Balance");
47    }  

48}

Program Output
Amortization program in Java output


Code Explanation:
1 include Scanner class to the program. This class is used to get user input from keyboad.
2 open AmortizationProgram class block.
3 open the main method.
4 declare local variable p, and iy to store amount of principle, and interest rate pery year.
5 delclare local variable n to store the number of years.
6 create Scanner object sc.
7-12 allow the user to input amount of loan, interest rate per year, and number of years.
13 call the calAmort method to calculate monthly payment and print the amortization schedule.
14 close the main method.
15 open calAmort method body
16 declare local variable newbal to store the new balance for each month.
17 declare local variable im to store the interest rate per month.
18 declare local variable nm to store the number of months.
19 declare local variables mp, ip, and pp to store monthly payment, interest paid, and principle paid for each month.
20 declare local variable i used in for loop iteration.
21 calculate monthly payment.
22 print heading text.
23-30 calculate interest paid(ip), principle paid (pp), old balance  (p),  new blance (newbal), and print these informaton. The for loop is used to print these informaton from the start month to the end of period except the last month. The for loop has the following form:
for(start;test;update){
//code to do something
}
The for loop exits when the test result is false. If the test result is true forever, the loop works infinitely.
The update has to properly define so that the loop will exit when ever you want.
31-36 calculate interest paid(ip), principle paid (pp), old balance  (p),  new blance (newbal) for the last month, and print these informaton by calling the printSch method.
For the last month, it is a special case. The principle paid is set to the new balance of the previous month. The monthly payment is set to sum of principle paid and interest paid, and the new balance is set to zero.
37 close the calAmort method.
38 open printSch method body.
39 define the code to print information.
40 close the printSch method.
41 open printHeader method body.
42-46 define code to print the heading text.
47 close the printHeader method.
48 close the AmortizationProgram class.


Note: 
1. To convert a numerical variable that is not a double type to double type, you has to place the double keyword in parentheses before that  numerical variable.
Example: convert from an integer variable to a double variable
double a;
int b=10;
a=(double)b;
2. The pow(double value,double power) of Math class is used to find the result of a value raised by its power.
Merge or Combine PDF, Txt, Images

2 comments: