Search This Blog

#Day18- #100DaysChallenge- Matlab Loops| Triangle Pattern with Characters

#Day18-Triangle Pattern with Characters

Task:
Print the given word in triangular form.
Example:
Matlab

m
ma
mat
matl
matla
matlab

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

Matlab Code:
function pattern_triangle(x)
    for i=1:1:length(x)
            for j=1:1:i
                fprintf('%s',x(j));
            end
                fprintf('\n');
    end


 Sample Input and Output
>> x='covid'

x =

    'covid'

>> pattern_triangle(x)
c
co
cov
covi
covid
>> x='elephant'

x =

    'elephant'

>> pattern_triangle(x)
e
el
ele
elep
eleph
elepha
elephan
elephant
>> x='laptop'

x =

    'laptop'

>> pattern_triangle(x)
l
la
lap
lapt
lapto
laptop

Click Here for Video Description

Join us on Telegram:https://t.me/matlabirawen
Join us on Facebook Group:https://www.facebook.com/groups/matlabcodes


No comments