Code in Matlab.
I need to make a diamond shape with *
I get part of it with the code below.
Instructions are: " Given the value of N print diamond of N + (N-1) rows. For example, if N = 5 it should print a diamond."
clc
clear
n = input ('number of rows \n')
o = input ('number of inverted rows \n')
t = (o-1)
for i = 1:n
for k = 1:n-i
fprintf (' ');
end
for j = 1:i
fprintf('* ');
end
fprintf('\n');
end
for q = k-1:o
for m = 1:o-q
fprintf(' ');
end
for j = 1:q
fprintf (' *');
end
fprintf('\n');
end
for r = 3:t
for r = 1: t-r
fprintf (' ')
end
for s = 1:r
fprintf ('*')
end
fprintf('\n');
end
Updated MATLAB Program:
% MATLAB Program that displays Diamon
clc;
clear;
% Reading number of rows
n = input('Number of rows: ');
% Iterating over rows
for i = 1:n
% For printing spaces
for k = 1:n-i
fprintf (' ');
end
% For printing Asterick
for j = 1:i
fprintf('* ');
end
fprintf('\n');
end
% Decrement n by 1 for lower triangle
n = n - 1;
% Code for printing below triangle
for i = n:-1:1
% For perinting spaces
for k = 1:n-i+1
fprintf (' ');
end
% For printing Asterick
for j = 1:i
fprintf('* ');
end
fprintf('\n');
end
__________________________________________________________________________________________
Sample Run:
Get Answers For Free
Most questions answered within 1 hours.