I need help writing a function that checks if a number is even or odd in Matlab.

Write a function that would decide whether a number is even or odd. The function will take input n and display on the command window either “even” or “odd”.
function [] = EvenOrOdd(n)
note that the function EvenOrOdd does not have a return value.
To display a string on the command window use disp(‘text’)
The function Mod(X,Y) returns the modulus after dividing X by Y
this is my work:
x=input ('value');
if mod(x,2)
disp('odd')
else
disp('even')
end
Help me please?

2 Commenti

What part were you hoping for assistance with?
The notes nearly give away the entire code.
I want to know how to write the code actually

Accedi per commentare.

 Risposta accettata

You can use mod function
mod(yournumber,2)

7 Commenti

But how will i include the strings?
The answer is in your question
disp('text')
Omg that's true xD thanks a lot
I actually tried it this way and it didn't work:
x=input ('value');
if mod(x,2)
disp('odd')
else
disp('even')
end
what should i do? this is my whole code
function tf = is_it_odd(n)
if 1 == rem(n,2)
tf = true;
else
tf = false;
end

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by