AND in while loop

6 ビュー (過去 30 日間)
Muhammad Althaafa Sukmaputra
Muhammad Althaafa Sukmaputra 2021 年 8 月 5 日
編集済み: Jonas 2021 年 8 月 5 日
why does this code stops at a=1 or b=1? i expect it to stop at a=1 and b=1
a=input('');
b=input('');
x=2;
c=1;
while (a~=1 && b~=1)
while (mod(a,x)==0) || (mod(b,x)==0)
c=c*x;
if (mod(a,x)==0)&&(mod(b,x)==0)
a=a/x;
b=b/x;
elseif mod(a,x)==0
a=a/x;
elseif mod(b,x)==0
b=b/x;
end
end
x=x+1;
end
disp(c)

回答 (1 件)

Jonas
Jonas 2021 年 8 月 5 日
編集済み: Jonas 2021 年 8 月 5 日

it should run if

   ~(a==1 && b==1)

which would equal

a~=1 || b~=1

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by