Search This Blog

#Day45 #100DaysChallenge- Matlab Loops|Diamond With Numbers

#Day45-Diamond With Numbers
Task:
Write a code to generate a diamond pattern,filled with numbers.Take user input for number of rows.
1
1 2
1 2 3
1 2 3 4
1 2 3 4
1 2 3
1 2
1

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.
Matlab code
function diamond_numbers(x)
k=x;
for i=1:1:x
    num=0;
    for hh=1:1:k
        fprintf('\t');
    end
    for j=1:1:i
        num=num+1;
        fprintf('\t');
        fprintf('%d',num)
        fprintf('\t');
    end
    k=k-1;
   fprintf('\n');
end
clear k i j hh
k=1;
for i=x:-1:1
    num=0;
    for hh=1:1:k
        fprintf('\t');
    end
    for j=1:1:i
        num=num+1;
        fprintf('\t');
        fprintf('%d',num);
        fprintf('\t');
    end
    k=k+1;
   fprintf('\n');
end



 Sample Input and Output

>> diamond_numbers(4)
1
1 2
1 2 3
1 2 3 4
1 2 3 4
1 2 3
1 2

1
Click Here for Video Description

No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();