How to print all prime numbers between 1 and 100 using a for loop?
24 ビュー (過去 30 日間)
古いコメントを表示
for f = (1:100)
f
isprime(f)
j = all(f)
end
fprintf('%j',j)
This is what I have, I either get j as logical or if I change it to "fprintf('%f', f)" I get f = 100.
I need to print the prime numbers.
What am I doing wrong?
0 件のコメント
採用された回答
KSSV
2020 年 10 月 28 日
編集済み: KSSV
2020 年 10 月 28 日
num = 1:100 ; % numbers till 100
idx = isprime(num) ; % get logical indices of prime numebrs
num(idx) % print the prime numbers
7 件のコメント
Stephen23
2020 年 10 月 28 日
The square brackets here are completely superfluous:
primenum = [num(idx)]
Get rid of them, they do absolutely nothing.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Install Products についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!