Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String[] args) {
- // all amounts of money in cents or pennies
- int p = 10000; // initial loan = $100.00
- int a = 2000; //monthly repayment = $20.00
- double i = 2.5; // monthly interest per cent
- int interest; // due each month
- int cumulativeInterest = 0;
- int unpaid = p;
- int payment;
- int monthCount = 0;
- while (unpaid > 0) {
- interest = (int) (unpaid * i / 100.0); // truncates cents
- cumulativeInterest += interest;
- unpaid += interest;
- payment = a;
- if (unpaid > payment) {// normal payment
- unpaid -= payment;
- } else { // final payment
- payment = unpaid;
- unpaid = 0;
- }
- monthCount++;
- System.out.println("Interest = " + interest / 100.0 + " Cumulative interest = " + cumulativeInterest / 100.0 + " Payment = " + payment / 100.0 + " Outstanding = " + unpaid / 100.0);
- }
- System.out.println("Paid off in " + monthCount+" months");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment