MATLAB: Nested Loop Script (Triangle)

nested loopscript

Write a nested llop script that will print the following pattern, printing one number or character at a time starting at 1 and using the numbers 0 through 9Screen Shot 2019-04-28 at 1.24.24 PM.png

Best Answer

Here is the complete answer to all parts of the assignment:
%%
clearvars; clc
%% Part 1.
k=1;
for ii = 1 : 5
for jj = 1 : ii
if k >10
k=k-10;
fprintf(num2str(k));
else
fprintf(num2str(k)) ;
end
k = k+1;
end
fprintf('\n') ;
end
%% Part 2
k = 65 ;
for ii = 1 : 5
for jj = 1 : ii
fprintf(char(k)) ;
k = k + 1 ;
end
fprintf('\n') ;
end
%% Part 3. Numbers
k=5;
for ii = 1 : 5
for jj = 1 : ii
if k >10
k=k-10;
fprintf(num2str(k));
else
fprintf(num2str(k)) ;
end
k = k+1;
end
fprintf('\n') ;
end
%% Part 3. Letters
k = 68 ;
for ii = 1 : 5
for jj = 1 : ii
fprintf(char(k) ) ;
k = k + 1 ;
end
fprintf('\n') ;
end