Page 1 of 1

Reputation:

# *Updated* Interest Rate Compiler

Posted 18 March 2010 - 05:46 AM

I have a program that i posted earlier, but it seems that either it seemed it was too broad to allow people to contribute, so I put in a loop to compile my paid interest and amount of months, however they do not build upon eachother, rather it just loops and continues the same statment infinitly. To explain what I'm going for, I want my program to determine how many months it would be to pay off a \$1000 loan at a 1.5% monthly interest rate, and pay a \$50 monthly payment. So as I build interest, I pay that interest off with my \$50 dollar payment, and then the rest goes towards my debt, leaving me a new balance that would carry onto the next month. I want to loop this until my balance equals zero, but I can't find a way to do so. Any help would be appreciated.

Is This A Good Question/Topic? 0

Reputation:

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 05:48 AM

```
import java.util.Scanner;

public class interest
{
public static void main( String args[] )
{

double price;
double rate;
double monthly;
double balance;
double interestpaid;
double result;
double x;
double total;
double differentmonth;
double diffinterestpaid;
double count;

Scanner input = new Scanner( System.in );

price = 1000;
rate = .015;
monthly = 50;
count=1;

do {
balance = (price + (price*rate) - 50);
interestpaid = (price*rate);

double count1;
result = (balance +(balance*rate) - 50);
diffinterestpaid = ((balance*rate) + interestpaid);

System.out.println("It will take " + result + " months to pay off the loan and \$" + diffinterestpaid + " interest will be paid");
count++;
} while (balance>0);

}
}

```

### #3 macosxnerd101

• Self-Trained Economist

Reputation: 9150
• Posts: 33,954
• Joined: 27-December 08

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 05:51 AM

You might want to check out the Compound Interest Calculator snippet I wrote. Also, in the future, please properly indent your code so we can more easily follow it. Thanks for helping us help you!

Reputation:

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 06:06 AM

I looked at the calculator, but Im having a hard time incorporating it into my code. I cant compile what I try and I'll admit that I'm lost. Could anyone give some advice on how to edit my code?

### #5 macosxnerd101

• Self-Trained Economist

Reputation: 9150
• Posts: 33,954
• Joined: 27-December 08

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 06:14 AM

Basically, my snippet uses the Compound Interest Formula. You can use it to calculate Compound Interest without looping, so in O(k) time with the formula vs. O(n) with a loop.

Reputation:

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 06:25 AM

The calculator is nifty dont get me wrong, but I dont know how many years its going to take for my balance to equal zero, the calculator allows me to determine the process if I know how many years my loan is for, but I dont have that information, that is what im trying to find. I could make an algebreic equation set time to x and make balance equal to zero, but im unsure how i would go about doing so.

### #7 macosxnerd101

• Self-Trained Economist

Reputation: 9150
• Posts: 33,954
• Joined: 27-December 08

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 06:33 AM

Are you familiar with exponential and logarithmic equations? You might want to set up a log equation to solve for time (t).

### #8 Dogstopper

Reputation: 2706
• Posts: 10,578
• Joined: 15-July 08

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 06:37 AM

```      do {
balance = (price + (price*rate) - 50);
interestpaid = (price*rate);

System.out.println(balance);

result = (balance +(balance*rate) - 50);
diffinterestpaid += interestpaid;
count++;
} while (balance>0.0);

```

I agree that you need to rethink your design because in order for there NOT to be an infinite loop, the condition must change (so that balance will at some point be 0). However, you never change balance except once, and after that, it remains the same. Personally, I think that you have way to many variables. Look into a logarithmic formula to solve this as you can directly find the time through a simple algebra

Reputation:

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 07:39 AM

So I guess I need to rethink my code but I want to salvage as much as possible, so I kept the following code..

```
import java.util.Scanner;

public class interest
{
public static void main( String args[] )
{

double price;
double rate;
double monthly;
double balance;
double interestpaid;
double count;

Scanner input = new Scanner( System.in );

price = 1000;
rate = .015;
monthly = 50;
count=1;

do {
balance = (price + (price*rate) - 50);
count++;
System.out.println("It will take " + (count++) + " months to pay off the loan");
break;
} while (balance>0.0);

do {

interestpaid = (price*rate);
count++;
System.out.println("and \$" + interestpaid + " interest will be paid");
break;
} while (price>0.0);
}
}

```

I know 960 months is way too high, which is what I get as an answer, I tried to start from scratch with the calculator shown but still no luck. I literally dont know where to go now .

I tried starting one to incorporate the calculator, but after the formula I dont know where to go. If the calculator is the best way to go, please guide me that way and ill start anew.

```import java.util.Scanner;

public class jaja {

public static void main( String args[] )

{

double principal;
double rate;
double numTimesCompound;
double years;
double x;

principal = 1000;
rate = 1.5;
numTimesCompound = 12;
years = x;

Math.pow(1+(rate/numTimesCompound));

}}

```

### #10 macosxnerd101

• Self-Trained Economist

Reputation: 9150
• Posts: 33,954
• Joined: 27-December 08

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 08:01 AM

macosxnerd101, on 18 March 2010 - 08:51 AM, said:

Also, take a look at the formula from the Wikipedia link, and try to make a logarithmic equation from it to solve for the appropriate variable.

After taking a look through your revised code, I wanted to talk about about these two lines. For the line years = x;, you will get a syntax error b/c x has not been initialized, so you can't assign its value to years. Also, Math.pow() accepts two params- the base and the exponent. So it should look like Math.pow(base, exponent). In addition, it returns a double value, so why are you invoking it on its own line, where the value returned won't be saved?
```years = x;
Math.pow(1+(rate/numTimesCompound));

```

Reputation:

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 10:00 AM

So I looked at some other codes that are kinda similar to the program im putting together and I got the following code...

```public class jaja{
public static void main( String args[] )
{
double time;
double principal=1000;
double rate=.015;
double amount=50;
for(int month=1;month<=900;month++){
amount=((rate*(Math.pow(1 + rate, month))-principal)/(Math.pow(1+rate,month) -1));
System.out.println (month + "    " + time);
}
}
}

```

The only thing is that I am trying to find time and the equation I have does not initialize time, so I am unsure an equation of singularly finding tiem itself.

Reputation:

## Re: *Updated* Interest Rate Compiler

Posted 18 March 2010 - 10:04 AM

Also, the days in the for() are merely just estimates i was running when intializing amount, but that would need to be fixed too, because the program that I need to make determines by itself how many months it will take to pay off the loan of 1000 dollars at 1.5% monthly paying 50 dollars a month, initially paying off interest and then paying off the debt. After I figure out how many months goes by, then i think i will be able to finish the second part of the program, which is to create a secondary program that determines how much interest was payed during the loan period.