In: Computer Science
Generate the first ten rows of Pascal triangle using MATLAB. Post code in the response
MATLAB Code to generate first ten rows of Pascal traingle :
function pt = pascal_triangle(n)
% first two rows of pascal traingle are constant
pt(1, 1) = 1;
pt(2, 1 : 2) = [1 1];
% If less or equal to 2 rows are requested, then we already created them and hence return.
if n < 3
return
end
for r = 3 : n
% first element is 1
pt(r, 1) = 1;
% In pascal triangle every element is the addition of the two elements
% present on the top of it.
for c = 2 : r-1
pt(r, c) = pt(r-1, c-1) + pt(r-1, c);
end
% last element of each row is 1
pt(r, r) = 1;
end
end
EXAMPLE :
INPUT: first ten
rows of pascal triangle

OUPTUT : ( NOTE : The zeros which appears in the ouput occurred naturally when we changed the row in our MATLAB program )
