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

Search This Blog

#Day77 #100DaysChallenge- Matlab Loops|Hollow Diamond

#Day77-Hollow Diamond

Task:

Write a code to Print a hollow Diamond pattern as shown below
  *
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab code 

function hollow_diamond(x)

k=x;

for i=1:1:x

    for hh=1:1:k

        fprintf('\t');

    end

    for j=1:1:i

        if j==1||j==i

        fprintf('\t');

        fprintf('*');

        fprintf('\t');

        else

            fprintf('\t');

            fprintf('\t');

        end

       

           

    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

        if j==1||j==i

        fprintf('\t');

        fprintf('*');

        fprintf('\t');

        else

             fprintf('\t');

            fprintf('\t');

        end

    end

    k=k+1;

   fprintf('\n');

end

 Sample Input and Output

>> hollow_diamond(4)

*

* *

* *

* *

* *

* *

* *

*


 Click here for video description


No comments

Popular Posts