Search This Blog

#Day46 #100DaysChallenge- Matlab Loops|Diamond With Alphabets

#Day46-Diamond With Alphabets
Task:
Write a code to generate a diamond pattern,filled with alphabets.Take user input for number of rows.
a
a b
a b c
a b c d
a b c d
a b c
a b

a

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_alphabets(x)
k=x;
al=['A':'Z'];
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('%s',al(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('%s',al(num));
        fprintf('\t');
    end
    k=k+1;
   fprintf('\n');
end


Sample Input and Output

>> diamond_alphabets(4)
a
a b
a b c
a b c d
a b c d
a b c
a b
a
>> diamond_alphabets(4)
A
A B
A B C
A B C D
A B C D
A B C
A B

A
Click Here for Video Description



Live Support: t.me/matlabirawen



No comments