0
Loan calculator
This code is supposed to display the remaining balance after 3 months, and the balance is decreased by 10% each month. I retraced the loop interations on paper, and with an input of 100,000 I get a remaining balance 65,610 which is wrong but very different from what this code shows 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 answer = 0; for (int x =1; x<=3; x++){ answer = amount - (amount*.10); amount -= answer; } System.out.println(amount); } }
8 RĂ©ponses
+ 1
I just figured it out, the answer has to be an integer number so
I only needed to divide by 10 instead of multiplying by .10 because that will give me a decimal point answer, here's the working code
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 / 10);
}
System.out.println(amount);
}
}
+ 1
double amount = scanner.nextDouble();
+ 1
Natanael ,
scanner.nextInt() or scanner.nextDouble()
both depend on the input wether you want an integer or a decimal
and you need to have a decimal because you have a multiplaction operation
which is multipling by 10% for the variable
or the integer if you want to display in a direct way.
I suggest you use decimal if you want a more precise answer.
Note:
there is no diffrence between them
.1 = .10
amount / 10 = amount * .1
0
I think I'm close now just need to figure out the problem between int and float variables, heres a better version of the code but I changed the amount variable to float, I get the right answer but with a decimal point
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
double interest = .10;
int answer = 0;
for (int x =1; x<=3; x++){
amount = amount - (amount*interest);
}
System.out.println(amount);
}
}
0
Natanael
You could simplify your logic by using an operator directly upon your amount variable
for(int i=0; i<3; i++){
amount *= 0.9;
}
System.out.println(amount);
0
Yes, I could simplify the logic within the loop which would mean less words but that's it, it will not make this code work at all, I think SoloProg has a better idea.
0
I take that back, this part here will not work because I'm not supposed to change it, originally it's int amount = scanner.nextInt()
This here won't work because it's above the section that says // your code starts here
/*******double amount = scanner.nextDouble();********/
0
Thank you, I knew about .1 = .10 but
dividing by 10 is equal to multiplying by .1 is something new I learned before I figure out this code challenge.
The challenge requires that the answer is displayed as an integer number, that's why I cannot use a float variable.
When I figured out this code the answer was wrong because the output was something like 72900.0
instead of just 72900
I think this is mostly a math problem, I figured it out once I used my calculator and decided to divide by 10