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

Search This Blog

#Day44 #100DaysChallenge- Matlab Loops|Pascal Triangle

#Day44-Pascal Triangle
Task:
Write a code to generate a pascal triangle .Take user input for number of rows.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

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

function pascaltriangle(x)
k=x-1;
 num=1;
for i=1:1:x
  
    for j=1:1:k
        fprintf('\t');
    end
        for j=1:1:i
           
            fprintf('\t');
            fprintf('%d',num(j));
            fprintf('\t');
        end
            k=k-1;
            fprintf('\n');
            zeroapp=[0,0];
            newvect=[zeroapp(1),num,zeroapp(end)];
            clear num;
            for i=1:1:length(newvect)-1
                num(i)=newvect(i)+newvect(i+1);
            end
end
end

 Sample Input and Output
>> pascaltriangle(5)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Click here for Video Description




Live Support: t.me/matlabirawen


            
               

           

No comments

Popular Posts