Im really awful at while loops and just loops in general . this is the code i ha
ID: 3912147 • Letter: I
Question
Im really awful at while loops and just loops in general . this is the code i have so far:
v =[];
i = 1
while v > 1000
v = i + rand()
end
i wasnt sure how to check divisibility? would you just divide the function by 3 or use rem or mod function?
Code i have so far:
x = magic(5);
y = 3;
mod(x,y)
Explanation / Answer
Note: Question 1 isn't clear. Could you please mention it clearly. BTW, one question at a time please -- Policy of Chegg.
Following is the code and sample output for part 2.
% code for part 2
x = magic(5)
y = 3;
fprintf("Numbers divisible by 3 are ");
for i = 1:5 % to loop through rows
for j = 1:5 % to loop through each number in row
if mod(x(i,j),3) == 0 % if it is divisible, printing
fprintf("%d ",x(i,j))
end
end
end
% Output
x =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Numbers divisible by 3 are 24 15 6 12 21 3 18 9
Note: Question 1 isn't clear. Could you please mention it clearly. BTW, one question at a time please -- Policy of Chegg.
Following is the code and sample output for part 2.
% code for part 2
x = magic(5)
y = 3;
fprintf("Numbers divisible by 3 are ");
for i = 1:5 % to loop through rows
for j = 1:5 % to loop through each number in row
if mod(x(i,j),3) == 0 % if it is divisible, printing
fprintf("%d ",x(i,j))
end
end
end
% Output
x =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Numbers divisible by 3 are 24 15 6 12 21 3 18 9