Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

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

Popular Posts

Followers