0

Loan calculator challenge

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); //your code goes here for (int month = 1; month <=6; month = month + 1){ int tax= amount /10; int newAmount= amount - tax; System.out.println(newAmount); } } } I got the code to print out the first month amount but i do not know how to get the equation to repeat

13th Dec 2020, 2:14 AM
Ben Szydlowski
33 Answers
+ 3
So Sovanrotha i did figure it out import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); //your code goes here for (int month = 1; month <=6; month = month + 1){ amount= (int)amount - (int)Math.ceil(0.1 * amount); } System.out.println(amount); } }
13th Dec 2020, 9:18 PM
Ben Szydlowski
+ 2
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amt = scanner.nextInt(); //your code goes here for(int i=0;i<6;i++) { amt=(int)amt-(int)Math.ceil(0.1*amt); } System.out.print(amt); } } Look at the problem then look at this you will understand what i did
13th Dec 2020, 5:13 AM
Coder
Coder - avatar
+ 2
https://code.sololearn.com/cp6GXE4Rc84v/?ref=app The reason why you get 54145 is because of Integer division. It ignores decimals (rounding down). Ultimately when you do a minus, you get an extra 1. 1: 100000 - 100000/10 = 90000 2: 90000 - 90000/10 = 81000 3: 81000 - 81000/10 = 72900 4: 72900 - 72900/10 = 65610 5: 65610 - 65610/10 = 59049 6: 59049 - 59049/10 = 59049 - 5904 (missing precision) = 54145 (extra 1, expected 54144) To bypass, you can do "amount = amount * 90 / 100". Do not hard-code -1 or +1. One good example will be 0. After 6 months, it will still be 0. By hard-coding -1, it results in -1 which is wrong.
16th Jan 2021, 4:40 AM
Lam Wei Li
Lam Wei Li - avatar
+ 1
《 Nicko12 》 thank you i removed the extra variable and got it to repeat the equation but the answer is off by 1 for some reason
13th Dec 2020, 3:11 AM
Ben Szydlowski
+ 1
And if i use int like the questiom says to all of the answers are 1 off
13th Dec 2020, 3:30 AM
Ben Szydlowski
+ 1
《 Nicko12 》 this is where i am at and the answers are off by 1
13th Dec 2020, 3:57 AM
Ben Szydlowski
+ 1
《 Nicko12 》 you did thank you!
13th Dec 2020, 4:49 AM
Ben Szydlowski
+ 1
Coder YES! That worked thank you so much!
13th Dec 2020, 7:14 AM
Ben Szydlowski
+ 1
Use 90% for 6 months to find the balance. 4 lines of code added. https://code.sololearn.com/cp6GXE4Rc84v/?ref=app If you are paying 10% every month, one way is to find the 10% and then use your total minus the 10% paid to get the balance. The other way, is to find the 90% so you don't have to minus. If you have 100 and you paid 10%, Method 1: 100 - (100 * 10%) = 90 Method 2: 100 * 90% = 90 The "i=0" is used for looping. It will loop from 0 until 5 (< 6), which is 6 times. Finally prints out amount.
14th Dec 2020, 2:20 PM
Lam Wei Li
Lam Wei Li - avatar
+ 1
Dennoun Jawad It's purposely hidden so no hard-coding can be done to cheat the system. At the same time, to strengthen problem-solving.
31st Dec 2020, 6:28 AM
Lam Wei Li
Lam Wei Li - avatar
+ 1
Ben Szydlowski that is exactly my problem, I don't get why it's off by 1
31st Dec 2020, 11:58 AM
Sabrina F.
Sabrina F. - avatar
+ 1
if you cast the system.out.println you dont need to subtract. println((int)amount) will convert it back to an int
18th Mar 2021, 5:27 AM
Jason Jenkins
Jason Jenkins - avatar
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); //your code goes here for (int month = 1; month <=3; month = month + 1){ int tax= amount /10; amount= amount - tax; } System.out.println(amount); } } This is the right a answer for loan calculator
26th Mar 2021, 10:09 AM
ANIKET SINGH
ANIKET SINGH - avatar
0
I used the double and found that the answer needs to round down and become an int
13th Dec 2020, 3:14 AM
Ben Szydlowski
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double amount = scanner.nextInt(); //your code goes here for (int month = 1; month <=6; month = month + 1){ double tax= amount /10; amount= amount - tax; } System.out.println(amount); } } This is where im at
13th Dec 2020, 3:15 AM
Ben Szydlowski
0
《 Nicko12 》 i do not know why but i got the first test correct but the 2nd is off by 1 still
13th Dec 2020, 3:29 AM
Ben Szydlowski
0
《 Nicko12 》 That code doesn't come out with the correct number
13th Dec 2020, 3:48 AM
Ben Szydlowski
0
Yeah, the number is so slightly off so it must be the math somewhere is wrong but i dont know where
13th Dec 2020, 3:55 AM
Ben Szydlowski
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); //your code goes here for (int month = 1; month <=6; month = month + 1){ int tax= amount / 10; amount= amount - tax; } System.out.println(amount); } }
13th Dec 2020, 3:56 AM
Ben Szydlowski
0
The problem i had was i over complicated it and put too many variables and had to just condense it into one variable and equation
13th Dec 2020, 9:18 PM
Ben Szydlowski