+ 3
Loan Calculator
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(double i =1; i<=6 ; i++){ amount -=(amount/10); } System.out.println(Math.round(amount)); } } ___________________ Hello, i donât know why this works just for two cases. Did anyone have an idea? I also tried it with Math.ceil() or turned double into Int but i think the amount must be rounded. Thanks!
12 Answers
+ 6
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.
+ 5
I tried with Math.ceil() and 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();
//my code start here
for (int i = 0; i < 6; i++){
amount -=(int)Math.ceil(amount*0.1);
}
System.out.print(amount);
}
}
+ 4
The amount was in this task as an integer declared. This should be not changed.
For answer of this question please read carefuly about operation on integer and double numbers. This will help you in addition:
https://code.sololearn.com/c4YDCGscyg92/?ref=app
+ 2
mert2, in your original code, everything is in Integer.
amount = amount - (amount / 100 * 10);
If amount is 150:
1) amount = 150 - (150 / 100 * 10)
2) amount = 150 - (1 * 10)
3) amount = 140 (instead of 135)
Note in step 2, 150 / 100 = 1 due to Integer flooring, which truncated the decimal.
You can do multiplication first than division to bypass this:
1) 150 * 10 / 100
2) 1500 / 100
3) 15
But this will only work until certain extent. If the number is 15 then it will no longer work.
1) 15 * 10 / 100
2) 150 / 100
3) 1 (instead of 1.5)
For second code, it is in Double.
amount -= amount * 0.10;
If amount is 150:
1) amount = 150 - 150 * 0.10
2) amount = 150 - 15
3) amount = 135
+ 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 x = 0; x < 3; x++) {
amount = amount - (amount / 100 * 10);
}
System.out.println(amount);
}
}
So the above was my original code, which worked for the first two cases. and then i have tried the answer given on this topic which was
for(int x = 3; x < 6; x++) {
amount -= amount * 0.10;
}
System.out.println(amount);
my current question is that why my code didn't work for the others. i mean they are the same thing.
what is the difference here? just asking to understand.
0
Maybe this is a dumb question, but... Is it correct to use the switch loop instead of the for one to solve this problem?
0
I solved it also with the switch loop. The code is longer and probably less elegant, but it is possible. This is my first time with Java
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 months = 3;
for (int i = 0; i < months; i++)
amount = amount * 90 / 100;
System.out.println(amount);
}
}
- 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 x = 3; x < 6; x++) {
amount -= amount * 0.10;
}
System.out.println(amount);
}
}
- 1
{
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 <3; i++)
amount = amount * 90 / 100;
System.out.println(amount);
}
}