Need help on the Loan Calculator for Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 4

Need help on the Loan Calculator for Java

https://code.sololearn.com/cXzemj7iFP2Y/?ref=app The expected output is 53144 but my output is 53145

30th Nov 2020, 6:48 AM
Daniel
49 Answers
+ 17
This code worked: 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 int rem_amt = amount; for (int i = 1 ; i <= 6 ; i++) { int paid = (int)Math.ceil(rem_amt); rem_amt = paid ; } System.out.println(rem_amt*729/1000); } }
9th Apr 2021, 3:23 AM
Aryan Singh Jadon
Aryan Singh Jadon - avatar
+ 15
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int payment=0; //your code goes here for (int i=0; i<3; i++){ payment= amount/10; amount-= payment; } System.out.println(amount); } }
22nd May 2021, 1:03 PM
Alaa Hegab
Alaa Hegab - avatar
+ 11
So pissed off! Needs elements it hasn't tougth you yet 😑
7th Jan 2021, 2:07 PM
Tommy
+ 11
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 i=1; i<4; i++){ int payment= (amount*10)/100; amount= amount-payment; } System.out.println(amount); } } my code totally worked and I made it as simple as I could
27th May 2021, 12:00 PM
Rajani Shrestha
Rajani Shrestha - avatar
+ 3
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 x = 0;x < 3;x++) { amount -= ((amount * 10) / 100); }; System.out.println(amount); } } //Simple
26th May 2022, 12:49 PM
IbrahimCPS
IbrahimCPS - avatar
+ 2
Here is tha answer, no need to import lang class. import java.util.Scanner; import java.lang.*; 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 i=0;i<6;i++){ int payment =(int)(10*amount )/100; if((amount%10)!=0){ amount =amount-1; } amount = amount- payment ; } System .out .print(amount); } }
24th Dec 2020, 5:49 AM
Priyabrata Mohanty
+ 1
Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int remAmount = amount; for (int i = 0; i < 6; i++) remAmount -= (remAmount * .10); System.out.println(remAmount);
10th Jan 2021, 3:10 AM
Mohamed
Mohamed - avatar
+ 1
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.
11th Jan 2021, 4:45 AM
Lam Wei Li
Lam Wei Li - avatar
+ 1
The issue of getting an incremented value instead of the intended one is directly connected with your usage of integers instead of doubles. If you receive a int value the java terminal outputs it after rounding, making it possible for incremented values to be present. There are two ways you can see through this issue: The first one is using 90% percent of the value instead of taking 10% than subtracting it from the rest, which is inevitably, and mathematically, the same thing. The only difference is you will receive cleaner code. import java.util.Scanner; class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double amount = scanner.nextInt(); for (int i = 0; i < 6; i++) { amount = amount * 0.9; if (i == 5) { int final_loan = (int)amount; System.out.println(final_loan); } } } } You can use this one here. This will be a great solution for some examples but the rest still yield an error, I tried but couldn't find any answer in the level of a beginner so you have to use math.ceil 2nd solution: Replacing the for loop with this in the previous code will yield the answer: int rem_amt = amount; for (int i = 1 ; i <= 6 ; i++) { int paid = (int)Math.ceil(10/100.0*rem_amt); rem_amt -= paid ;
3rd Feb 2021, 6:17 AM
Deniz
Deniz - 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 i=0;i<6;i++){ amount = (int)(amount*0.9); } System.out.println(amount); } }
10th Mar 2021, 3:04 AM
Igor Garcia
Igor Garcia - 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 int i=0; int Newamount; while(i<6){ amount=amount*90/100; ++i; if(i==5){ Newamount=amount*90/100; System.out.println(Newamount); } } } }
17th Mar 2021, 4:48 PM
Grace Oyoo
Grace Oyoo - avatar
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //your code goes here int amount = scanner.nextInt(); int i = 1; while (i <= 3) { amount = (int)(amount *0.9); i++; } System.out.println(amount); } }
19th Jun 2021, 5:45 AM
Aashiq Husaain
Aashiq Husaain - avatar
0
cmdr tigerray1, The question in SoloLearn is: "Use a loop to calculate the payment and remaining amounts for each month. Also, use integers for amounts." Didn't state for the 10% (or 90%) intermediate factors can't be double. However, final result must be in integer. If you realised, in another solution above, the poster used "Math.ceil(10/100.0*rem_amt)". The denominator, 100.0, is a double as well. If you want to avoid using 0.9, you can use 90/100.0 in my coded solution. It's the same.
7th Dec 2020, 3:57 AM
Lam Wei Li
Lam Wei Li - avatar
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 int payment = 0; for (int x=0; x<=5; x++) {payment = amount/10; amount = amount-payment;} {System.out.println (amount); } }} This is what I came up with. It works but my end output is 54145, but it expects 54144. I honestly think there's an issue with the imbedded compiler
7th Jan 2021, 4:05 PM
cmdr tigerray1
cmdr tigerray1 - avatar
0
The problem with this is there's limitations. This assignment does not use any special class/ method calls, and the numbers have to be int. Normally you could use decimal numbers for the math, but this forces the compiler to look for a double that isn't there. Go to java > conditionals and loops>10 end of ... to see the parameters. don't use anything that hasn't been taught to that point
11th Jan 2021, 12:26 AM
cmdr tigerray1
cmdr tigerray1 - avatar
0
Ah I see, tyvm
11th Jan 2021, 5:59 AM
cmdr tigerray1
cmdr tigerray1 - avatar
0
Ok
13th Jan 2021, 8:06 AM
AYYAPARAJA K
AYYAPARAJA K - avatar
0
OK
13th Jan 2021, 8:19 AM
AYYAPARAJA K
AYYAPARAJA K - avatar
0
int amount = scanner.nextInt(); for (int i = 0; i < 6; i++){ // type cast amount to a double double payment = (double)amount / 10; double remaining = amount - payment; //type cast amount back to an int amount = (int)remaining; } System.out.println(amount); /* this was amount always begins a new iteration or the loop as an int but is calculated as a double so the decimal parts are subtracted correctly, and eliminating the need to use Math.floor or Math.ceil. */
21st Jan 2021, 1:34 AM
David Sinclair
David Sinclair - avatar
0
Easiest solution! Not a single loop is used. It is what a novice should do.....ha ha ha import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int fm=(int)Math.ceil(amount-1-amount*0.1); int sm=(int)Math.ceil(fm-1-fm*0.1); int tm= (int)Math.ceil(sm-1-sm*0.1); int fom=(int)Math.ceil(tm-1-tm*0.1); int fim=(int)Math.ceil(fom-1-fom*0.1); int sim=(int)Math.ceil(fim-fim*0.1); System.out.println(sim); } }
7th Mar 2021, 3:36 PM
Md Nayeem Mustakim