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

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

Popular Posts

Followers