Java course loan calculation not running | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java course loan calculation not running

Hello guys I have tried by adding int and double of the variable type for loan calculation exercise in java course. Test case passing but not making tick ark. And it shows my programme is not success.i have add int for programme but and is adding by 1. Finally I convert it to double and cast the resulting intro zeiling value is rounded up import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double amount = scanner.nextDouble(); double amount1 =0; for(int i=1;i<7;i++){ amount1 = (amount *10)/100; amount =amount -amount1; } System.out.println((int)amount); } }

8th Dec 2020, 11:01 AM
Gayan Suranga
Gayan Suranga - avatar
18 Answers
+ 4
Ok,by my opinion for this the problem coming is in the implicit conversion of int and double in your code .Try to get your inputs in int type and calculate by double than you can change the final amount in int and display the results. Try using my code : import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); double ra; //your code goes here for(int i=0;i<6;i++) { double x=(10.0*a)/100.0; ra=a-x; a=(int)ra; } System.out.println(a); } This works fine enough to get all the tests correct.
10th Dec 2020, 6:56 AM
Abhinav Raj
Abhinav Raj - avatar
+ 4
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:24 PM
Lam Wei Li
Lam Wei Li - avatar
+ 3
If you 0bserve the sample, then it says need to round up the amount1.. Edit : I mean on amount1, not on amount. Gayan Suranga You need amount = amount - Math.ceil(amount1) ;
8th Dec 2020, 2:15 PM
Jayakrishna 🇮🇳
+ 2
Gayan Suranga casting to int causes truncation of the decimal. It actually lowers the value to the floor. To round it up to the ceiling value you should use Math.ceil().
8th Dec 2020, 2:48 PM
Brian
Brian - avatar
+ 2
Please see my new update import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double amount = scanner.nextDouble(); double amount1 =0; for(int i=1;i<7;i++){ amount1 = (amount *10)/100; amount =(int)amount -(Math.ceil(amount1)); } System.out.println(amount); } } Result comes with one d3 imal value but test are showing g not pass
9th Dec 2020, 1:59 AM
Gayan Suranga
Gayan Suranga - avatar
+ 1
I found that the test cases are flawed. If you use a proper interest calculation some of the tests fail. They are looking for a summation of the monthly *ceiling* values.
8th Dec 2020, 7:26 PM
Brian
Brian - avatar
+ 1
Have anyone finished answering it?please check my code. https://code.sololearn.com/ckbyObI2Ilz3/?ref=app
9th Dec 2020, 4:49 PM
Danziel Cempron
+ 1
Danziel Cempron see my code import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double amount = scanner.nextDouble(); double amount1 =0; for(int i=1;i<7;i++){ amount1 = (Math.ceil(amount *10/100)); amount =(int)amount-amount1; } System.out.println((int)amount); } } this is working fine
9th Dec 2020, 5:10 PM
Gayan Suranga
Gayan Suranga - avatar
+ 1
Danziel Cempron see your line number 10 is wrong just check the calculation it has to be int amount1 = (Math.ceil(amount *10/100));
9th Dec 2020, 5:14 PM
Gayan Suranga
Gayan Suranga - avatar
+ 1
Gayan Suranga Math class is like far away from the loan calculator. So you have to finish the math class first before answering the probem? It should be indicated there that you have to use math class cause its like many lessons away from there.
10th Dec 2020, 1:53 AM
Danziel Cempron
+ 1
100 000 * 10 / 100 = 10 000 100 000 / 10 = 10 000 The same amount
10th Dec 2020, 1:59 AM
Danziel Cempron
+ 1
The (int)amount makes no sense inside a for loop
10th Dec 2020, 2:01 AM
Danziel Cempron
+ 1
And you also have to learn typecasting which is like in second to the last lesson
10th Dec 2020, 2:10 AM
Danziel Cempron
+ 1
Check my code it's more simplified https://code.sololearn.com/ckbyObI2Ilz3/?ref=app
10th Dec 2020, 2:11 AM
Danziel Cempron
+ 1
Dear all I have done this and corrected what I have missed. Thank for all your support
10th Dec 2020, 5:42 PM
Gayan Suranga
Gayan Suranga - avatar
+ 1
Hi everyone, Here there is my code, it works. 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 double res= amount; int y= 6; int i=0; for( i=0; i<y; i++) { res= res- (Math.ceil(res*10/100)); } System.out.println((int)res); } }
16th Dec 2020, 9:33 PM
Erika Martello
Erika Martello - avatar
0
thants why i jave cast double into int
8th Dec 2020, 2:17 PM
Gayan Suranga
Gayan Suranga - avatar
0
Brian i have done different way no solution could you plz tell me the code that i have shared instead of
8th Dec 2020, 5:25 PM
Gayan Suranga
Gayan Suranga - avatar