Search This Blog

#Day29 #100DaysChallenge- Matlab Loops| Square from two Triangle


#Day29-Square from two Triangle

Task:
Print Square from two Triangle as shown below by taking user input for the number of lines or rows
Note: Show the difference between two triangles by using two special characters.

        * * * * * &
* * * * & &
* * * & & &
* * & & & &

* & & & & &

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

Matlab code
function twotraingled_square(x)
k=0;
for i=x:-1:1
    k=k+1;
    for j=1:1:i
     
        fprintf('\t');
        fprintf('*');
    end
   for jj=1:1:k
       fprintf('\t');
        fprintf('&');
   end
  
fprintf('\n');
end
       

Sample Input and Output
>> twotraingled_square(7)
* * * * * * * &
* * * * * * & &
* * * * * & & &
* * * * & & & &
* * * & & & & &
* * & & & & & &
* & & & & & & &
>> twotraingled_square(9)
* * * * * * * * * &
* * * * * * * * & &
* * * * * * * & & &
* * * * * * & & & &
* * * * * & & & & &
* * * * & & & & & &
* * * & & & & & & &
* * & & & & & & & &
* & & & & & & & & & 


Click here for Video Description



No comments