Hello,
Firstly, use the rem function to get the remainders of every value of 'i' with 3 & 5 - r3 = rem(i,3);
r5 = rem(i,5);
Next, use if-else loops to work with different possible scenarios. Use logical operators to get desired outputs -
if ~(r3 || r5)
disp([num2str(i),' is divisible by 3 AND 5']);
elseif (r3 && r5)
disp([num2str(i),' is NOT divisible by 3 or 5']);
else
if ~r3
disp([num2str(i),' is divisible by 3']);
else
disp([num2str(i),' is divisible by 5']);
end
end
References:
- if-else-elseif statements: Link
- disp function: Link
- Logical operators: Link
Finally, MATLAB Onramp courses are a great way to begin learning MATLAB fundamentals. Find link here.
Hope this helps!
Best Answer