フィルターのクリア

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

21 ビュー (過去 30 日間)
Miriah Dudley
Miriah Dudley 2021 年 3 月 30 日
コメント済み: Miriah Dudley 2021 年 3 月 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 Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by