Need Help With Program - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Need Help With Program

Posts: 1Member
This program is supposed to calculate how long it will take to pay off a credit card. The method I have written theoretically should work and is:
public void creditCard()
{
System.out.println("Credit Card Pay-Off Schedule");
int numMonths=0;
while (principal>0){
numMonths++;
principal+=(principal*(percent/12));
monthlyPmt=.03*principal;
if(monthlyPmt<10){
monthlyPmt=10;
}
if(monthlyPmt>principal){
monthlyPmt=principal;
}
principal-=monthlyPmt;
}
double totalpayments=(principal*Math.pow(1+(percent/100),(numMonths/12)));
System.out.println("Loan Amount: "+principal);
System.out.println("Yearly Interest: "+percent);
System.out.println("Payback Months: "+numMonths);
System.out.println("Total Payments: "+totalpayments);
System.out.println("Total Interest: "+(totalpayments-principal));
}
But it doesn't. It just freezes.

• Posts: 10Member
**double totalpayments=(principal*Math.pow(1+(percent/100),(numMonths/12)));

-- i don't what is this for? but I assume you are just trying to get total how much you paid all-in-all including interest.. correct??

**percent (variable)

-- you forget to define the value of this variable.. I assuming this is the interest impose by the bank per annumm..

**System.out.println("Loan Amount: "+principal);

-- you tried to print the original value of the principal amount, but remember you modified its value in the while loop..

-- I rewrote your program.. I hope its correct.. Although I am not sure if the computation is correct.. good luck!!

public class creditCard {

public static void main( String args[])
{
double principalAmount=3000, monthlyPayment=0, totalPay = 0;
final double percent = 4; // assuming a 4 percent interest per annum
int numMonths=0;

double InitialPrincipalAmount = principalAmount;

while(principalAmount>0){
numMonths++;
principalAmount = principalAmount + (principalAmount*((percent/100)/12));
monthlyPayment = principalAmount * .03; // 3 percent monthly? of the principal?

if( monthlyPayment < 10)
monthlyPayment = 10;

totalPay = totalPay + monthlyPayment;

principalAmount = principalAmount - monthlyPayment;

}

System.out.println("Loan Amount: "+InitialPrincipalAmount);
System.out.println("Yearly Interest: "+ (percent) + "%");
System.out.println("Payback Months: "+numMonths);
System.out.println("Total Payments: "+totalPay);
System.out.println("Total Interest: "+(totalPay-InitialPrincipalAmount));

}
}