Search This Blog

#Day57 #100DaysChallenge- Matlab Loops|Floyd's Triangle

#Day57-Floyd's Triangle

Task:

Write a code to Print Floyd's Triangle.Get user Input for Number of rows.
1
01
101
0101
10101

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

Matlab code

function trainglewith01(x)

 for i=1:1:x
     if mod(i,2)==0
         k=0;
         d=1;
         for j=1:1:i
             fprintf('%d',k)
             temp=k;
             k=d;
             d=temp;
         end
     else
         k=1;d=0;
         for j=1:1:i
             fprintf('%d',k)
             temp=k;
             k=d;
             d=temp;
         end
     end
    
        
fprintf('\n');
end

Sample Input and Output
>> trainglewith01(6)

1
01
101
0101
10101
010101
>> trainglewith01(5)
1
01
101
0101
10101
>> trainglewith01(10)
1
01
101
0101
10101
010101
1010101
01010101
101010101
0101010101


Click here for Video description

No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();