Script to generate a number and show if it is even or odd

21 次查看(过去 30 天)
Write a set of commands (a script) that will do the following:
(a) Generate one random integer between 1 and 1000 and assign it to the variable mynum (see built-in
command randi).
(b) Find the remainder when mynum is divided by 2 and assign it to the value myrem (see built-in command
rem).
(c) Using if-else statements and myrem, check and print out whether mynum is even or odd.
(You may use any display or print option of your choice.)
This is the script I have so far:
myrem = rem(number,2);
number = randi(1000);
my = myrem;
if my == 0
disp('Even');
elseif my == 1
disp('Odd');
and when I try to 'call' it my = myrem it reads back that it is " Unrecognized function or variable 'myrem'. " Could anyone help me figure why my script won't work?

采纳的回答

David Hill
David Hill 2021-3-30
number = randi(1000);%use must generate the number first before using it
myrem = rem(number,2);
%my = myrem; no need for this
if myrem == 0
disp('Even');
else%if my == 1 only two possibilities
disp('Odd');
end%don't forget to end
  1 个评论
Miriah Dudley
Miriah Dudley 2021-3-30
Thank you for your time!! Can you explain why I wouldn't need to include my = myrem? Just curious :) thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by