Search This Blog

#Day25 #100DaysChallenge- Matlab Loops| Diamond


#Day25-Diamond

Task:
Print Diamond shape as shown below by taking user input for number of lines or rows
                4 Row Diamond
                                                                                *            
                                                                *                             *            
                                                *                             *                             *            
                                *                             *                             *                             *            
                                *                             *                             *                             *            
                                                *                             *                             *            
                                                                *                             *            
                                                                                *            
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(x)
k=x;
for i=1:1:x
    for hh=1:1:k
        fprintf('\t');
    end
    for j=1:1:i
        fprintf('\t');
        fprintf('*');
        fprintf('\t');
    end
    k=k-1;
   fprintf('\n');
end
clear k i j hh
k=1;
for i=x:-1:1
    for hh=1:1:k
        fprintf('\t');
    end
    for j=1:1:i
        fprintf('\t');
        fprintf('*');
        fprintf('\t');
    end
    k=k+1;
   fprintf('\n');
end
       

Sample Input and Output

>> diamond(3)
                                                                *            
                                                *                             *            
                                *                             *                             *            
                                *                             *                             *            
                                                *                             *            
                                                                *             

Click here for Video Description





Live Support: t.me/matlabirawen



MATLAB Book for the beginner: https://amzn.to/3fTfmTa

1 comment: