Azzera filtri
Azzera filtri

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

21 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by