Effacer les filtres
Effacer les filtres

why if else condition is not working inside for loop

2 vues (au cours des 30 derniers jours)
MD.MAINUL ISLAM
MD.MAINUL ISLAM le 16 Juil 2020
Commenté : MD.MAINUL ISLAM le 19 Juil 2020
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');
}
}

Réponse acceptée

Jyotirmay Mishra
Jyotirmay Mishra le 16 Juil 2020
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

Plus de réponses (1)

Walter Roberson
Walter Roberson le 16 Juil 2020
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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by