Write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.
The following variables contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
Assume that the interest is compounded monthly according to the balance at the end of the month (after the payment for that month is made). The monthly payment must be a multiple of $10 and is the same for all months. Notice that it is possible for the balance to become negative using this payment scheme, which is okay. A summary of the required math is found below:
Monthly interest rate = (Annual interest rate) / 12.0
Monthly unpaid balance = (Previous balance) - (Minimum monthly payment)
Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)
=================================================
Test Case 1:
Enter your balance: 3329
Enter your Annual Interest Rate: 0.2
Lowest Payment: $310
Test Case 2:
Enter your balance: 4773
Enter your Annual Interest Rate: 0.2
Lowest Payment: $440
Test Case 3:
Enter your balance: 3926
Enter your Annual Interest Rate: 0.2
Lowest Payment: $360
Start with $10 payments per month and calculate whether the balance will be paid off in a year this way (be sure to take into account the interest accrued each month - this means 10 * 12 won't work).If $10 monthly payments are insufficient to pay off the debt within a year, increase the monthly payment by $10 and repeat.
This is what I have and I cant get the program to equal the lowest payment in the test cases when I put in the balance and interest rate. I also have to use the nested loop.
import java.util.Scanner;
public class MinPayment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your balance: ");
double balance = input.nextDouble();
System.out.print("Enter your Annual Interest Rate: ");
double rate = input.nextDouble();
double i = 1;
rate = rate / 12.0;
double payment = 0;
while (i <= 12) {
i++;
balance = balance + (rate * balance);
while (balance >= 0) {
payment += 10;
balance = balance - payment;
}
}
System.out.printf("Lowest Payment: $%.0f", payment);
}
}
No comments:
Post a Comment