Search This Blog

#Day34 #100DaysChallenge- Matlab Loops| Hollow Diamond-3

#Day33- Hollow Diamond-3

 

Task:

Print  hollow diamond  as shown below by taking user input for the number of lines or rows

                *             *             *             *             *             *             *             *             *             *

                *             *             *             *                                             *             *             *             *

                *             *             *                                                                             *             *             *

                *             *                                                                                                             *             *

                *                                                                                                                                             *

                *                                                                                                                                             *

                *             *                                                                                                             *             *

                *             *             *                                                                             *             *             *

                *             *             *             *                                             *             *             *             *

                *             *             *             *             *             *             *             *             *             *            

               

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those

Matlab code

function hollowdiamond(x)

k=0;

for i=x:-1:1

    for j=1:1:i

        fprintf('\t');

        fprintf('*');

      

    end

    for g=1:1:k

        fprintf('\t');

    end

       for sj=1:1:i

           fprintf('\t');

       fprintf('*');

       end

       fprintf('\n');

       k=k+2;

end

k=x+(x-2);

 

for i=1:1:x

    for j=1:1:i

        fprintf('\t');

        fprintf('*');

      

    end

    for g=1:1:k

        fprintf('\t');

    end

       for sj=1:1:i

           fprintf('\t');

       fprintf('*');

       end

       fprintf('\n');

       k=k-2;

end

end

 

Sample Input and Output

>> hollowdiamond(5)

                *             *             *             *             *             *             *             *             *             *

                *             *             *             *                                             *             *             *             *

                *             *             *                                                                             *             *             *

                *             *                                                                                                             *             *

                *                                                                                                                                             *

                *                                                                                                                                             *

                *             *                                                                                                             *             *

                *             *             *                                                                             *             *             *

                *             *             *             *                                             *             *             *             *

                *             *             *             *             *             *             *             *             *             *



No comments