why if else condition is not working inside for loop
1 回表示 (過去 30 日間)
古いコメントを表示
MD.MAINUL ISLAM
2020 年 7 月 16 日
コメント済み: MD.MAINUL ISLAM
2020 年 7 月 19 日
var n=25;
for(i=2;i<n;i++){
if(n%i==0){
console.log('this is not prime');
}
else{
console.log('this is prime');
}
}
0 件のコメント
採用された回答
Jyotirmay Mishra
2020 年 7 月 16 日
Your code is not a MATLAB code
To do something like this in MATLAB
n = 25;
for i =1:n
if(mod(n,i)==0)
disp('this is not prime');
else
disp('this is prime');
end
end
0 件のコメント
その他の回答 (1 件)
Walter Roberson
2020 年 7 月 16 日
The if condition is working the way you programmed it.
If you want to test for something being prime, you need to test for all of the non-prime possibilities first, and you should only display that the number is prime after you have finished ruling out the possibility of a divisor.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!