+ 1

Loan Calculator

Hey. Ive been struggling with this one. Any ideas why I cant use int instead of double? Expected behaviour is to reduce the remaining sum owed each iteration. This works when using double. When I convert everything to int it returns the same sum for each iteration. import java.util.Scanner; public class Program { public double calculatePercentage(double obtained, double percentage) { // double 10percent = (double)(value*(percentage/100.0f)); double remainder = obtained - (double)(obtained*(percentage/100)); return remainder; } public static void main(String[] args) { //instantiate the pc method Program pc = new Program(); //take a input starting amount Scanner scanner = new Scanner(System.in); System.out.println("Enter an amount: "); double obtained = scanner.nextDouble(); int i = 1; do { double remains = pc.calculatePercentage(obtained, 10.0); System.out.println("The remaining amount of your loan is: " + remains); obtained = pc.calculatePercentage(remains,10.0); i++; } while (i<3); } }

27th Apr 2022, 6:50 AM
Shaya Rand
Shaya Rand - avatar
2 Réponses
+ 3
You need to use this way calculatePercentage method if you use 100 instead of 100.0 then 10/100 = 0 that is why you get same print statement. -------------------------------------------------------------------- public int calculatePercentage(int obtained, int percentage) { int remainder = obtained - (int)(obtained*(percentage/100.0)); return remainder; } ------------------------------------------------------------------------ Also you can use below code import java.util.Scanner; public class Test { public int calculatePercentage(int obtained, int percentage) { int remainder = obtained - (int)(obtained*(percentage/100.0)); return remainder; } public static void main(String[] args) { //instantiate the pc method Test pc = new Test(); //take a input starting amount Scanner scanner = new Scanner(System.in); System.out.println("Enter an amount: "); int obtained = scanner.nextInt(); int remainder = obtained; for(int i = 0; i<3; i++){ remainder = pc.calculatePercentage(remainder, 10); } System.out.println(remainder); } }
27th Apr 2022, 11:15 AM
Thineshan Panchalingam
Thineshan Panchalingam - avatar
0
Great jobs 👌👌👌
27th Apr 2022, 7:06 AM
Dhanjit Kumar Singh
Dhanjit Kumar Singh - avatar