How to print all prime numbers between 1 and 100 using a for loop?
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Elijah McNeil
am 28 Okt. 2020
Kommentiert: KSSV
am 28 Okt. 2020
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 Kommentare
Akzeptierte Antwort
KSSV
am 28 Okt. 2020
Bearbeitet: KSSV
am 28 Okt. 2020
num = 1:100 ; % numbers till 100
idx = isprime(num) ; % get logical indices of prime numebrs
num(idx) % print the prime numbers
7 Kommentare
Stephen23
am 28 Okt. 2020
The square brackets here are completely superfluous:
primenum = [num(idx)]
Get rid of them, they do absolutely nothing.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!