필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by