using input form from user, use for and while loops for the number and the shape provided in matlab

To create a program that takes input from the user and uses for and while loops to display the desired number and shape in Matlab, you can follow the steps below:

  1. Use the input function to get the desired number from the user.

  2. Use a for loop to display the number of rows specified by the user. Within the loop, use another loop to display the shape repeatedly in the same row.

  3. Use a while loop to display the shape in a diagonal line. Within the loop, use another loop to increment the row and column for each iteration until reaching the desired number.

Here's an example code that takes input from the user and displays a triangle as well as a diagonal line using for and while loops:

main.m
% Get input from user
num = input('Enter a number: ');

% Display triangle using for loop
disp('Triangle:')
for i = 1:num
    for j = 1:i
        fprintf('*')
    end
    fprintf('\n')
end

% Display diagonal line using while loop
disp('Diagonal line:')
row = 1;
col = 1;
while row <= num && col <= num
    for j = 1:col
        fprintf(' ')
    end
    fprintf('*\n')
    row = row + 1;
    col = col + 1;
end
411 chars
25 lines

gistlibby LogSnag