Loan calculator challenge | Sololearn: Learn to code for FREE!

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

12/13/2020 2:14:16 AM

Ben Szydlowski

33 Answers

New Answer

+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); } }

+2

《 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

+2

《 Nicko12 》 you did thank you!

+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

+2

Coder YES! That worked thank you so much!

+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.

+1

And if i use int like the questiom says to all of the answers are 1 off

+1

《 Nicko12 》 this is where i am at and the answers are off by 1

+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.

+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.

+1

Ben Szydlowski that is exactly my problem, I don't get why it's off by 1

+1

if you cast the system.out.println you dont need to subtract. println((int)amount) will convert it back to an int

+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

0

I used the double and found that the answer needs to round down and become an int

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

0

《 Nicko12 》 i do not know why but i got the first test correct but the 2nd is off by 1 still

0

《 Nicko12 》 That code doesn't come out with the correct number

0

Yeah, the number is so slightly off so it must be the math somewhere is wrong but i dont know where

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); } }

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