필터 지우기
필터 지우기

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 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