-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathP5_22.java
More file actions
32 lines (26 loc) · 1.12 KB
/
Copy pathP5_22.java
File metadata and controls
32 lines (26 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*Write a method that computes the balance of a bank account with a given initial balance
and interest rate, after a given number of years. Assume interest is compounded yearly.*/
import java.util.Scanner;
public class P5_22 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter initial balance: ");
double initialBalance = input.nextDouble();
System.out.print("Please enter interest rate: ");
double interestRate = input.nextDouble();
System.out.print("Please enter years: ");
int years = input.nextInt();
input.close();
System.out.printf("Balance is: %.2f", balance(initialBalance, interestRate, years));
}
public static double balance(double initialBalance, double interestRate, int years) {
double interest = initialBalance * (interestRate / 100.0);
double balance = initialBalance + interest;
while (years > 0) {
balance += interest;
interest = balance * (interestRate / 100.0);
years--;
}
return balance;
}
}