How to print all prime numbers between 1 and 100 using a for loop?
조회 수: 122 (최근 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?
채택된 답변
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에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!