why if else condition is not working inside for loop
Ältere Kommentare anzeigen
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');
}
}
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 16 Jul. 2020
2 Stimmen
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.
1 Kommentar
MD.MAINUL ISLAM
am 19 Jul. 2020
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!