Filter löschen
Filter löschen

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

21 Ansichten (letzte 30 Tage)
Miriah Dudley
Miriah Dudley am 30 Mär. 2021
Kommentiert: Miriah Dudley am 30 Mär. 2021
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?

Akzeptierte Antwort

David Hill
David Hill am 30 Mär. 2021
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 Kommentar
Miriah Dudley
Miriah Dudley am 30 Mär. 2021
Thank you for your time!! Can you explain why I wouldn't need to include my = myrem? Just curious :) thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by