Search This Blog

#Day30 #100DaysChallenge- Matlab Loops| Printing Number Eight


#Day30-Printing Number Eight
 Task:
Print number EIGHT as shown below by taking user input for the number of lines or rows

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

Matlab code
function printingeight(x)

for i=1:1:x
     if i==1||i==ceil(x/2)||i==x
         for j=1:1:floor(x/2)
             fprintf('\t');
             fprintf('*');
         end
     else
                 fprintf('*');
                 for j=1:1:floor(x/2)+1
                     fprintf('\t');
                 end
                     fprintf('*');
     end
                     fprintf('\n');
end


end

Sample Input and Output
>> printingeight(5)
* *
* *
* *
* *
* *
>> printingeight(9)
* * * *
* *
* *
* *
* * * *
* *
* *
* *
* * * *
>> printingeight(20)
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *

Click Here for Video Description



No comments