Having troube with java, i am not a regular programmer, if anybody can help me out writting this program:
write a program that takes two numbers from the java console representing, respectively, an investment and an interest rate(you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in notation) the Future value of the investment in 5, 10 and 20 years using the following formula:
Future value= investment * (1+interest rate) year
interest is assume annual rate and compounded annually.
Sure, I can help you with that. Here's a step-by-step guide on how to write the program you described:
Step 1: Import the required package for user input:
```java
import java.util.Scanner;
```
Step 2: Create a new Scanner object to read input from the console:
```java
Scanner scanner = new Scanner(System.in);
```
Step 3: Prompt the user to enter the investment amount and interest rate:
```java
System.out.print("Enter the investment amount: ");
double investment = scanner.nextDouble();
System.out.print("Enter the annual interest rate (in decimal form, e.g., 0.065): ");
double interestRate = scanner.nextDouble();
```
Step 4: Calculate and display the future value for 5, 10, and 20 years:
```java
int[] years = { 5, 10, 20 };
for (int i = 0; i < years.length; i++) {
int year = years[i];
double futureValue = investment * Math.pow(1 + interestRate, year);
System.out.println("Future value after " + year + " years: " + futureValue);
}
```
Step 5: Close the Scanner:
```java
scanner.close();
```
Putting it all together, here's the complete code:
```java
import java.util.Scanner;
public class FutureValueCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the investment amount: ");
double investment = scanner.nextDouble();
System.out.print("Enter the annual interest rate (in decimal form, e.g., 0.065): ");
double interestRate = scanner.nextDouble();
int[] years = { 5, 10, 20 };
for (int i = 0; i < years.length; i++) {
int year = years[i];
double futureValue = investment * Math.pow(1 + interestRate, year);
System.out.println("Future value after " + year + " years: " + futureValue);
}
scanner.close();
}
}
```
You can now run this program, enter the investment amount and interest rate when prompted, and it will calculate and display the future value for 5, 10, and 20 years.