While loop for loan repayment?
6 views (last 30 days)
Show older comments
Karan Sandhu
on 11 Mar 2016
Commented: Karan Sandhu
on 12 Mar 2016
In the picture shown, I have to basically create each loan payment table underneath its respective black line. Although I was able to create the first two tables, I am having trouble with two things: The first is finding the "Sum of Interest Paid" and "Sum of Payments" at the bottom of each table; the second problem is actually creating the third table. Any assistance would be appreciated. Here is my code:
InitialBalance = input('Enter the initial balance in US$ : '); % 10,000,3000,9000
% if initial balance is more than or equal to 5000, then interest rate is
% 2%. Anything else is 1%
if InitialBalance>=5000
rate=0.02;
fprintf('\nYour minimum monthly payment is $%g\n',rate*InitialBalance+1)
Minimum=rate*InitialBalance+1;
else
rate=0.01;
fprintf('\nYour minimum monthly payment is $%g\n',rate*InitialBalance+1)
Minimum=rate*InitialBalance+1;
end
MonthPay = input('Enter the Monthly Payment in US$ : '); % 12000, 4000, 2000
month=0; % initialize months
Payment=0; % initialize payment
Interest=0;
fprintf('\nMonths \t Interest($) \t Payment($) Balance($)\n')
fprintf(' %2i %2i %2i %2i\n',month,Interest,Payment,InitialBalance)
Balance=InitialBalance;
while MonthPay>=Minimum % initiates while loop if and only if monthly payment >= minimum payment
while Balance~=0
month=month+1;
Payment=MonthPay;
Interest=rate*Balance;
Payment=Interest+Balance;
if Balance-Payment<0
Balance=0;
else
Balance=Balance-Payment;
end
fprintf(' %2i %2i %2i %2i\n',month,Interest,Payment,Balance)
end
end
fprintf('\nSum of Interest Paid = $%b\n')
fprintf('\tSum of Payments = $%b')
0 Comments
Accepted Answer
Image Analyst
on 11 Mar 2016
Edited: Image Analyst
on 12 Mar 2016
Your final two fprintf()'s don't have any numerical values to be put into the %b. You need to have a variable sumOfPayments and sumOfInterest and increment that inside the loop by the payment and interest for that iteration, after you've initialized it to zero before the loop of course.
And I wouldn't use %i and %b. b is not even anything. I would use %d for integers and %.2 for fractional real numbers.
3 Comments
Image Analyst
on 12 Mar 2016
Try this:
InitialBalance = input('Enter the initial balance in US$ : '); % 10,000,3000,9000
% if initial balance is more than or equal to 5000, then interest rate is
% 2%. Anything else is 1%
if InitialBalance>=5000
rate=0.02;
fprintf('\nYour minimum monthly payment is $%g\n',rate*InitialBalance+1)
Minimum=rate*InitialBalance+1;
else
rate=0.01;
fprintf('\nYour minimum monthly payment is $%g\n',rate*InitialBalance+1)
Minimum=rate*InitialBalance+1;
end
MonthPay = input('Enter the Monthly Payment in US$ : '); % 12000, 4000, 2000
theMonth=0; % initialize months
Payment=0; % initialize payment
Interest=0;
fprintf('\nMonths \t Interest($) \t Payment($) Balance($)\n')
fprintf(' %2d %11.2f %15.2f %11.2f\n',theMonth,Interest,Payment,InitialBalance)
Balance=InitialBalance;
loopCounter = 1;
sumOfPayments = 0;
sumOfInterest = 0;
maxIterations = 20;
% initiates while loop if and only if monthly payment >= minimum payment
while MonthPay >= Minimum && Balance~=0 && loopCounter <= maxIterations
theMonth = theMonth + 1;
Payment = MonthPay;
Interest = rate*Balance;
% Get the sum of these so far.
sumOfPayments = sumOfPayments + MonthPay;
sumOfInterest = sumOfInterest + Interest;
Balance = Interest + Balance;
if Balance-Payment<0
Balance = 0;
else
Balance = Balance-Payment;
end
fprintf(' %2d %11.2f %15.2f %11.2f\n',...
theMonth, Interest, Payment, Balance)
loopCounter = loopCounter + 1;
end
fprintf('\nSum of Interest Paid = $%.2f\n', sumOfInterest)
fprintf('\tSum of Payments = $%.2f\n', sumOfPayments)
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!