1. Member
Join Date
Feb 2013
Posts
13
Rep Power
0

## How to add loop statement

Hi everyone,

I am in need of finishing up my problem. Before I ask my questions here is the problem:

Write a method that computes future investment value at a given interest rate for a specified number of years.

Write a test program that prompts the user to enter the investment amount (e.g., 1000) and the interest rate (e.g., 9%) and prints a table
that displays future value for the years from 1 to 30, as shown below:

I need to create a loop to show years (1-30), as I already have gotten the other codes. Here is what I have so far.

Java Code:
```public static void main(String[] args) {

// Step 0: create a Scanner for input
Scanner input = new Scanner(System.in);

// Step 1: enter investment amount
System.out.print("Enter investment amount: ");
double investmentAmount = input.nextDouble();

// Step 2: enter annual interest rate
System.out.println("Enter annual interest Rate, e.g. 4.25: ");
double annualInterestRate = input.nextDouble();

// Step 3: calculate monthly rate from annual interest rate
double monthlyInterestRate = annualInterestRate / 1200 ;

// Step 4: enter number of years
System.out.print(
"Enter number of years as an integer, for example 1: ");
int numberofYears = input.nextInt();

// Step 5: compute future value
double futureInvestmentValue = investmentAmount * Math.pow(1 + (annualInterestRate * .01
/12),numberofYears * 12);

// Step 6: output future value
System.out.println("Future value with interest compunded monthly:" + "\$"
+ futureInvestmentValue + ".");

}
}```
Thanks guys!

2. ## Re: How to add loop statement

Originally Posted by Hawks
Before I ask my questions ...

db

3. Member
Join Date
Feb 2013
Posts
13
Rep Power
0

## Re: How to add loop statement

Sorry if I was confusing. My question is how would I start/make the loop to display the 30 future value results.

4. Senior Member
Join Date
Jan 2009
Location
NJ, USA
Posts
261
Rep Power
6

## Re: How to add loop statement

First you'd want some method to calculate the future value for a single year. Then, you loop from 0 to 30 and call that method as needed. Something like this:

Java Code:
```public static void main(String[] args)
{
double[] amount = new double[30];
double[] annual = new double[30];
double[] monthly = new double[30];
//fill data from input...

System.out.println("Years \t\t Future Value");
for(int i = 0; i < 30; i++)
{
System.out.println(i + " \t\t " + futureValue(amount[i], annual[i], monthly[i], i));
}
}

public static double futureValue(double amount, double annual, double monthly, int years)
{
double value = 0.0;
// calculate future value based on data
return value;
}```
Last edited by AndrewM16921; 02-24-2013 at 10:58 PM.

5. Member
Join Date
Feb 2013
Posts
13
Rep Power
0

## Re: How to add loop statement

Thank you!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•