# Thread: Array and Loop Problem

1. Member
Join Date
Sep 2011
Posts
2
Rep Power
0

## Array and Loop Problem

Java Code:
```public class Monthly_Array {

public static void main(String[] args) {

double loanAmount=200000.00;
double[] rate = {.0535, .055, .0575};
int[] term = {84, 180, 360};

double balance = loanAmount;
double monthlyInterest = 0;
double monthlyPrincipal = 0;
double monthlyPayment = 0;
int i;

for (i = 0; i < term.length; i++){

monthlyPayment = (loanAmount * (rate[i]/12))/(1-1/Math.pow((1 + rate[i]/12),(term[i])));
monthlyInterest = balance * (rate[i]/12);
monthlyPrincipal = monthlyPayment - monthlyInterest;
balance -= monthlyPrincipal;

System.out.print("Term: " + term[i] + " Months:" );
System.out.print(" The Monthly Payment is: ");
System.out.printf( "%,.2f" , monthlyPayment );
System.out.print("  The Interest Rate is: ");
System.out.printf( "%,.2f",  monthlyInterest );
System.out.print("  The Principal is: ");
System.out.printf( "%,.2f",  monthlyPrincipal );
System.out.print("  The Balance is: ");
System.out.printf( "%,.2f",  balance  );
System.out.println();
}

}

}```

3. Member
Join Date
Sep 2011
Posts
2
Rep Power
0

## Re: Array and Loop Problem

Thanks for your help but unfortunately that does not solve my problem and I accidently posted the wrong code it scrolls to the end, which is 360 but at the end of 84, 180, and 360 the balance should be 0.00 and reset for the next term, but that is not happening and I cant figure out the problem. Please Help anything will be Appreciated!!

Java Code:
``` import java.util.Scanner;
public class Amortization_Array2 {

/**
* @param args
*/
public static void main(String[] args) {

double a = 200000.00;
double[] i = {0.0535, 0.055, 0.0575};
int[] n = {84, 180, 360};

i = new double [3];
n = new int [3];

i[0] = 0.0535;    n[0] = 84;
i[1] = 0.055;     n[1] = 180;
i[2] = 0.0575;    n[2] = 360;

Scanner keyboard = new Scanner (System.in);
double r = (1+i[0]/12);
double monthlyPayments  = a * ( r - 1 ) / ( 1 - Math.pow(r,-n[0]));
int cnt = 0;
for ( cnt = 0; cnt <= 84; cnt++)

{
double interest = a * ( i[0]/12 );
r = (1+i[0]/12);
double principal = monthlyPayments - interest ;
double balance = a - principal;

a = balance;

System.out.println("  Payment Number: " + cnt);
System.out.print("The monthly Payment Amount is: " );
System.out.printf( "%,.2f" , monthlyPayments );
System.out.print("  The Interest is: " );
System.out.printf( "%,.2f",  interest );
System.out.print("  The Principal Payment is: ");
System.out.printf( "%,.2f",  principal );
System.out.print("  The Balance is: ");
System.out.printf( "%,.2f",  balance );

if (cnt % 24 == 0)
{
System.out.println();
System.out.println("  Press Enter To Continue...");
keyboard.nextLine();

r = (1+i[1]/12);
monthlyPayments  = a * ( r - 1 ) / ( 1 - Math.pow(r,-n[1]));
for ( cnt = 0; cnt <= 180; cnt++)

{   interest = a * ( i[1]/12 );
principal = monthlyPayments - interest ;
balance = a - principal;

a = balance;

System.out.println("  Payment Number: " + cnt);
System.out.print("The monthly Payment Amount is: " );
System.out.printf( "%,.2f" , monthlyPayments );
System.out.print("  The Interest is: " );
System.out.printf( "%,.2f",  interest );
System.out.print("  The Principal Payment is: ");
System.out.printf( "%,.2f",  principal );
System.out.print("  The Balance is: ");
System.out.printf( "%,.2f",  balance );

if (cnt % 24 == 0)
{
System.out.println();
System.out.println("  Press Enter To Continue...");
keyboard.nextLine();

r = (1+i[2]/12);
monthlyPayments  = a * ( r - 1 ) / ( 1 - Math.pow(r,-n[2]));
for ( cnt = 0; cnt <= 360; cnt++)

{   interest = a * ( i[2]/12 );
principal = monthlyPayments - interest ;
balance = a - principal;

a = balance;

System.out.println("  Payment Number: " + cnt);
System.out.print("The monthly Payment Amount is: " );
System.out.printf( "%,.2f" , monthlyPayments );
System.out.print("  The Interest is: " );
System.out.printf( "%,.2f",  interest );
System.out.print("  The Principal Payment is: ");
System.out.printf( "%,.2f",  principal );
System.out.print("  The Balance is: ");
System.out.printf( "%,.2f",  balance );

if (cnt % 24 == 0)
{
System.out.println();
System.out.println("  Press Enter To Continue...");
keyboard.nextLine();

}
}
}
}
}
}
}
}```