Click here to Skip to main content
15,887,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package programming101;

import java.util.Scanner;

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

Scanner myScanner = new Scanner(System.in);

int Deposit;
int Interest;
int Year1;
int Year2;
int Year3;
int Year4;

System.out.print("Enter your deposit amount in Singapore dollars ($SGD): ");
Deposit = myScanner.nextInt();

System.out.print("Enter the fixed compound interest rate as a percentage (%): ");
Interest = myScanner.nextInt();

Year1 = Deposit + (Deposit / 100 * Interest);
System.out.println("After 1 year you will have: $" + Year1);

Year2 = Year1 + (Year1 / 100 * Interest);
System.out.println("After 2 year you will have: $" + Year2);

Year3 = Year2 + (Year2 / 100 * Interest);
System.out.println("After 3 year you will have: $" + Year3);

Year4 = Year3 + (Year3 / 100 * Interest);
System.out.println("After 4 year you will have: $" + Year4);

}
}

run:
Enter your deposit amount in Singapore dollars ($SGD): 100
Enter the fixed compound interest rate as a percentage (%): 20
After 1 year you will have: $120
After 2 year you will have: $140
After 3 year you will have: $160
After 4 year you will have: $180
BUILD SUCCESSFUL (total time: 12 seconds)

What I have tried:

Year1 = Deposit + (Deposit / 100 * Interest);
Year2 = Year1 + (Year1 / 100 * Interest);
Year3 = Year2 + (Year2 / 100 * Interest);
Year4 = Year3 + (Year3 / 100 * Interest);
Posted
Updated 13-Feb-18 2:34am
v2

1 solution

Integers are probably not a good thing to use here: any fractional part of a result is discarded as they only hold whole numbers.
So if you start with a deposit of 200, and interest of 10%, then:
Year1 = 200 + (200 / 100 * 10) == 200 + (2 * 10) == 220
Year2 = 220 + (220 / 100 * 10) == 220 + (2 * 10) == 240
and so on - because the "0.2" parts of 220 / 100 is discarded.

Change all your variable to floating point values, and it should start to work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900