フィルターのクリア

How to check two conditions?

4 ビュー (過去 30 日間)
Omar B.
Omar B. 2022 年 2 月 9 日
コメント済み: Omar B. 2022 年 2 月 11 日
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

採用された回答

David Hill
David Hill 2022 年 2 月 9 日
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 件のコメント
Omar B.
Omar B. 2022 年 2 月 11 日
I am working with just scalars. In my code I used & not &&.
Omar B.
Omar B. 2022 年 2 月 11 日
Thank you so much. I got it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by