Search This Blog

#Day43 #100DaysChallenge- Matlab Loops|Triangle Pattern With Number

#Day43-Triangle Pattern With Number
Task:
Write a code to generate a triangular pattern with number.Take user input for number of rows.
        1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.
Matlab code



function triangle_patternwithbumber(x)
k=x-1;
for i=1:1:x
    num=0;
    for j=1:1:k
        fprintf('\t');
    end
        for j=1:1:i
            num=num+1;
            fprintf('\t');
            fprintf('%d',num);
            fprintf('\t');
        end
            k=k-1;
            fprintf('\n');
end
           
 Sample Input and Output
>> triangle_patternwithbumber(5)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5



Click here for Video Description





No comments