Write an m-file that will: For integer numbers between 1 and 30, check for and r
ID: 3535783 • Letter: W
Question
Write an m-file that will:
For integer numbers between 1 and 30, check for and report
numbers that are
It does not need to report other cases (such as numbers that are
divisible by only 2).
When run, your program should display lines similar to
these:
"3 is divisible by 3, but not divisible by 2"
"6 is divisible by both 2 and 3"
"9 is divisible by 3, but not divisible by 2"
Take the following hints into consideration in writing your
program:
from n = 1 to 30
value,
o calculate the remainder after division by 2 by using the rem
function
o calculate the remainder after division by 3 by using the rem
function
o create a string variable strn by converting the number n to a
string using num2str
function (this will be useful in displaying the required
lines)
o Use an if-end structure with proper relational and conditional
operators (and the
calculated remainder values) to properly implement the
conditional cases
mentioned above.
o Use the disp command to display the required lines on the
command window.
Explanation / Answer
for i=0:30
a[i]=input('enter the numbers')
end
for i=0:30
if((a(i)%2==0)&&(a(i)%3!=0))
disp('number is divisble by 2 but not by 3')
end
if((a(i)%3==0)&&(a(i)%2!=0))
disp('number is divisble by 3 but not by 2')
end
if((a(i)%2==0)&&(a(i)%3==0))
disp('number is divisble by both 2 aand 3')
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.