How to check two conditions?

1 Ansicht (letzte 30 Tage)
Omar B.
Omar B. am 9 Feb. 2022
Kommentiert: Omar B. am 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

Akzeptierte Antwort

David Hill
David Hill am 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 Kommentare
Omar B.
Omar B. am 11 Feb. 2022
I am working with just scalars. In my code I used & not &&.
Omar B.
Omar B. am 11 Feb. 2022
Thank you so much. I got it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays 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