fprintf results into 2 columns

4 views (last 30 days)
Braden
Braden on 30 Nov 2022
Commented: Braden on 30 Nov 2022
Hoping someone can help me with the fprintf function... I'm trying to display the results of this into 2 even columns but its taking the results and stacking them....
Looking for the Interest_p and payment variables to display in 2 even lines...
clc; clear; close all;
principal = input('Enter Principal in Dollars: ');
years = input('Enter Number of years: ');
interest=0.0025:0.0025:0.07;
interest_m=interest/12;
n=years*12;
interest_p=interest*100;
payment=principal.*interest_m.*((interest_m+1).^n)./(((1+interest_m).^n)-1);
fprintf('****************************************************\n\n')
fprintf(' MORTGAGE PAYMENT CALCULATIONS\n\n')
fprintf(' Interest Rate Monthly Payment\n')
fprintf(' %3.2f %7.2f\n',interest_p,payment)
fprintf('****************************************************')
  2 Comments
Stephen23
Stephen23 on 30 Nov 2022
Edited: Stephen23 on 30 Nov 2022
Without any loop is easy with a minor modification to your code:
principal = 1000;%input('Enter Principal in Dollars: ');
years = 5;%input('Enter Number of years: ');
interest=0.0025:0.0025:0.07;
interest_m=interest/12;
n=years*12;
interest_p=interest*100;
payment=principal.*interest_m.*((interest_m+1).^n)./(((1+interest_m).^n)-1);
%fprintf('****************************************************\n\n')
%fprintf(' MORTGAGE PAYMENT CALCULATIONS\n\n')
%fprintf(' Interest Rate Monthly Payment\n')
fprintf(' %3.2f %7.2f\n',[interest_p;payment]) % changed this line only
0.25 16.77 0.50 16.88 0.75 16.99 1.00 17.09 1.25 17.20 1.50 17.31 1.75 17.42 2.00 17.53 2.25 17.64 2.50 17.75 2.75 17.86 3.00 17.97 3.25 18.08 3.50 18.19 3.75 18.30 4.00 18.42 4.25 18.53 4.50 18.64 4.75 18.76 5.00 18.87 5.25 18.99 5.50 19.10 5.75 19.22 6.00 19.33 6.25 19.45 6.50 19.57 6.75 19.68 7.00 19.80
%fprintf('****************************************************')
Braden
Braden on 30 Nov 2022
Oh, that's simple! Thanks Stephen!

Sign in to comment.

Accepted Answer

VBBV
VBBV on 30 Nov 2022
clc; clear; close all;
principal = 1000;%input('Enter Principal in Dollars: ');
years = 5;%input('Enter Number of years: ');
interest=0.0025:0.0025:0.07;
interest_m=interest/12;
n=years*12;
interest_p=interest*100;
payment=principal.*interest_m.*((interest_m+1).^n)./(((1+interest_m).^n)-1)
payment = 1×28
16.7728 16.8793 16.9863 17.0937 17.2016 17.3099 17.4186 17.5278 17.6373 17.7474 17.8578 17.9687 18.0800 18.1917 18.3039 18.4165 18.5296 18.6430 18.7569 18.8712 18.9860 19.1012 19.2168 19.3328 19.4493 19.5661 19.6835 19.8012
fprintf('****************************************************\n\n')
****************************************************
fprintf(' MORTGAGE PAYMENT CALCULATIONS\n\n')
MORTGAGE PAYMENT CALCULATIONS
fprintf(' Interest Rate Monthly Payment\n')
Interest Rate Monthly Payment
for k = 1:length(payment)
fprintf(' %3.2f %7.2f\n',interest_p(k), payment(k))
end
0.25 16.77 0.50 16.88 0.75 16.99 1.00 17.09 1.25 17.20 1.50 17.31 1.75 17.42 2.00 17.53 2.25 17.64 2.50 17.75 2.75 17.86 3.00 17.97 3.25 18.08 3.50 18.19 3.75 18.30 4.00 18.42 4.25 18.53 4.50 18.64 4.75 18.76 5.00 18.87 5.25 18.99 5.50 19.10 5.75 19.22 6.00 19.33 6.25 19.45 6.50 19.57 6.75 19.68 7.00 19.80
fprintf('****************************************************')
****************************************************

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!