if else Problem in matlab

1 回表示 (過去 30 日間)
MD Anower Hossain
MD Anower Hossain 2019 年 10 月 30 日
編集済み: Bhaskar R 2019 年 10 月 30 日
clear;
clc;
a = input('enter a number ');
result = isdivby4(a);
disp(result);
function out = isdivby4(n)
if n%2 == 0
out = 1;
elseif n%2 == 1
out = 0;
end
end
% what is the problem here ? alltime i get the output is 1 if i enter 3 then i should get 0 . whats wrong here??

採用された回答

Bhaskar R
Bhaskar R 2019 年 10 月 30 日
編集済み: Bhaskar R 2019 年 10 月 30 日
In C/C++ progromming language the symbol '%' used for remining value but in the MATLAB the percentile symbol(%) used as commnet. In MATLAB if conditioning other than 0 for all values it is true so you got always 1 for your function
MATLAB is very rich in documentation please read MATLAB basics
It can be optimized in one line statement rather than using as function
result = (mod(a, 4) == 0)
  1 件のコメント
MD Anower Hossain
MD Anower Hossain 2019 年 10 月 30 日
yes ,, got it ,, thank you so much

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by