Clear Filters
Clear Filters

How to check two conditions?

4 views (last 30 days)
Omar B.
Omar B. on 9 Feb 2022
Commented: Omar B. on 11 Feb 2022
How can I check two conditions in if and elseif statment? When I run the following code, I got the last sentence " Your input is negative and even number "
function output=even_or_odd(n)
n = 'Insert a number: ';
x = input(n);
if x>=0 & rem(n,2)==0
disp('Your input is positive and even number ');
elseif x>=0 & rem(n,2)~=0
disp('Your input is positive and odd number ');
elseif x<0 & rem(n,2)==0
disp('Your input is negative and even number ');
else
disp('Your input is negative and even number ');
end
end

Accepted Answer

David Hill
David Hill on 9 Feb 2022
function output=even_or_odd()
x = input('Insert a number: ');
if mod(x,2)==0&&x>=0
output='Your input is positive and even number';
elseif mod(x,2)==1&&x>=0
output='Your input is positive and odd number';
elseif mod(x,2)==0&&x<0
output='Your input is negative and even number';
else
output='Your input is negative and odd number';
end
end
  5 Comments
Omar B.
Omar B. on 11 Feb 2022
I am working with just scalars. In my code I used & not &&.
Omar B.
Omar B. on 11 Feb 2022
Thank you so much. I got it.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!