+ 1

Java. What's wrong with my code

You take a loan from a friend and need to calculate how much you will owe him after 6 months. You are going to pay him back 10% of the remaining loan amount each month. Create a program that takes the loan amount as input, calculates and outputs the remaining amount after 6 months. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int nomonths = 1; int inst; int rem; while(nomonths<=6) {inst=(amount *10)/100; rem=amount-inst; amount=rem; nomonths++; } System.out.println(amount); } }

20th Jan 2021, 10:04 AM
mangesh choudhari
mangesh choudhari - avatar
1 Answer
0
Have you actually tried it? What kind of input do you enter, where you expect a different output? Your program works well with big numbers, but when the input is small, the result will be strange. Your amount, inst and rem variables should be double type, instead of int. That way you can make more precise calculations with small numbers.
3rd Apr 2021, 5:21 AM
Tibor Santa
Tibor Santa - avatar