In: Computer Science
For this question, you must show all work to receive any
credit.
DONOT USE MATLAB FOR THIS QUESTION.
for k = 3*1:6:21
j = k/3;
if rem(j,6) >= 3
disp(k)
elseif rem(j,3) < 2
disp(j)
else
disp(3)
end
end
write out the solution
Here I am giving an explanation for the code:
Firstly for loop is given as
k=3*1:6:21 => k=3:6:21 =>k=3,3+6,3+6+6,21 => k=3,9,15,21.
So for loop is iterated 4 times with k= k=3,9,15,21.
Next j=k/3
So j becomes j=1,3,5,7(3/3,9/3,15/3,21/3) after iterationg for loop j changes from starting to ending value.
So loop repeats 4 times
First for k=3 and j=1:
if rem(j,6)>=3
rem(1,6)=1/6 remainder is 1 which is not true so it goes to next ifelse
rem(j,3)<2
rem(j,3)=1 it is less than 2 hence,True. So this ifelse executes and disp(j) is exuted and 1 is shown in output.
Second for k=9 and j=3:
if rem(j,6)>=3
rem(3,6)=3/6 remainder is 3 which is true. So this if executes and disp(k) is exuted and 9 is shown in output.
Thirdly for k=15 and j=5:
if rem(j,6)>=3
rem(5,6)=5/6 remainder is 5 which is true. So this if executes and disp(k) is exuted and 15 is shown in output.
Finally for k=21 and j=7:
if rem(j,6)>=3
rem(7,6)=21/6 remainder is 1 which is not true so it goes to next ifelse
rem(j,3)<2
rem(7,3)=1 it is less than 2 hence,True. So this ifelse executes and disp(j) is exuted and 7 is shown in output.
So output is:
1
9
15
7