how to create a 1,12,123,1234 pattern in a triangle
Show older comments
How do I make a pattern from 1:N, Lets say N = 5, so it turns out like this:
1
12
123
1234
12345
Accepted Answer
More Answers (3)
Image Analyst
on 28 Jun 2015
If you want a simple "for" loop way of printing this to the command window, try this:
N = 5
for k = 1 : N
% Print this line in the command window.
fprintf('%d ', 1:k);
% Scroll down to the next line.
fprintf('\n');
end
4 Comments
Mikaila Denise Loanzon
on 4 Apr 2020
Edited: Image Analyst
on 4 Apr 2020
I tried your code and it worked. How about if I wanted the output to look like this?
n = 4
1
12
123
1234
Thank you!
Les Beckham
on 4 Apr 2020
Try this:
N = 5
fmt = sprintf('%%%dd', N); % format to print right-justified with N characters width
for k = 1 : N
str = sprintf('%d', 1:k); % numbers 1 through k as a string
% Print this line in the command window.
fprintf(fmt, str2num(str));
% Scroll down to the next line.
fprintf('\n');
end
Image Analyst
on 4 Apr 2020
Try this:
fprintf('n = 4\n'); % Print the n=4 line.
vec = [1,12,123,1234]; % Some data...
for k = 1 : length(vec)
% Print a space followed by a field of 4 for the number.
fprintf(' %4d\n', vec(k));
end
n = 4
1
12
123
1234
Why not go one step further?
fprintf('n = 4\n'); % print a fake assignment echo
vec = {' 1',' 12',' 123','1234'}; % if we're going to precalculate results ...
fprintf('%s\n',vec{:}); % homework complete!
No wait!
result = 'n = 4\n 1\n 12\n 123\n1234\n';
fprintf(result); % that was way easier!
I know this is not the only one of these threads, but I'm just going to throw this here anyway. Since I don't really care about meeting unspecified requirements of junk homework, I choose to assume my own.
I want to support n>9; in order to facilitate that, we need to prevent ambiguity by adding spaces between numbers. I choose to assume that formatting is based on a fixed field width instead of a fixed inter-field pad width. I also want to support both right and left justification of the overall line, but within each internal field, the numbers should always be right-justified. Lastly, since I think the point of this exercise is basic string formatting, I don't want to rely on the output behavior of celldisp() or similar; likewise, I don't want a bunch of array indices or other chaff printed between each output line.
Here's what I have. I'm not great with wringing elegance out of fprintf(), but it does what I expected it to do.
% n,minpad both must be positive nonzero integers
triprint(20,1,'left')
triprint(20,1,'right')
function triprint(n,minpad,justify)
% synopsis goes here ...
% validate inputs or provide defaults ...
% calculate the internal field format expression
ifw = max(ceil(log10(abs(double(fix(1:n)))+1))) + minpad; % field width
ifmt = sprintf('%%%dd',ifw); % always right-justified
for k = 1:n
rowstr = sprintf(ifmt,1:k); % block of right-justified fixed-width number fields
switch lower(justify)
case 'right'
strpad = repmat(' ',1,ifw*n - numel(rowstr)); % block of spaces
fprintf('%s%s\n',strpad,rowstr(minpad+1:end)); % print both, stripping leading pad
case 'left'
% no point adding trailing pad blocks
fprintf('%s\n',rowstr(minpad+1:end)); % print it, stripping leading pad
otherwise
error('this is an error message')
end
end
end
Categories
Find more on Characters and Strings 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!