using input form from user, use for and while loops for the number and the shape provided by the user to make a triangle shape in matlab

Here is an example code that takes input from the user for the number of rows in the triangle and the shape of the triangle. We use a for loop to iterate over the rows and a while loop to create the desired shape of the triangle.

main.m
% input form for number of rows and shape of triangle
num_rows = input('Enter the number of rows: ');
shape = input('Enter the Shape (1 for increasing, 2 for decreasing): ');

% create triangle shape
row = 1;
if shape == 1
    while row <= num_rows
        for col = 1:row
            fprintf('*');
        end
        fprintf('\n');
        row = row + 1;
    end
elseif shape == 2
    while num_rows >= row
        for col = 1:num_rows
            fprintf('*');
        end
        fprintf('\n');
        num_rows = num_rows - 1;
    end
else
    fprintf('Invalid Shape\n');
end
581 chars
26 lines

This code takes input from the user for the number of rows and the shape of the triangle. Based on the shape input, it uses a while loop to create the triangle shape. If the shape is increasing, we iterate through rows using a while loop and for each row we print the necessary number of asterisks using another for loop. If the shape is decreasing, we iterate through the rows again but this time, we print the asterisks in descending order using a similar while and for loop. Finally, if an invalid shape is provided, the program alerts the user.

gistlibby LogSnag