i dont what is the problem in this simple program plz help distinguish prime numbers

n=input ('the num ');
if n==1 ;
disp('the num is prime ')
break
else
for i=n:sqrt(n);
if rem(n,i)==0;
disp('the num is not prime ')
else
disp('the num is prime')
end
end
end

 Respuesta aceptada

the cyclist
the cyclist el 20 de Dic. de 2013
Editada: the cyclist el 20 de Dic. de 2013
Two errors I see:
Your "break" statement should be a "return" statement.
Where you have
for i=n:sqrt(n)
you should have
for i=2:sqrt(n)
I think.
When you make those changes, you actually get into your for loop properly, and you will then see that your algorithm is flawed. :-(

1 comentario

ok thnx there is another problem but not technical its in the idea anyway i made another i find another successful program num=input('the num '); j=0 if num~=0 for i=2:sqrt(m); if rem(n,i)==0; j=j+1; end end if j~=0; disp('the num is not prime'); else disp('the num is prime') end else('num is prime') end

Iniciar sesión para comentar.

Más respuestas (1)

if isprime(num)
disp('The num is prime')
else
disp('The num is not prime')
end

Etiquetas

Preguntada:

el 20 de Dic. de 2013

Comentada:

el 20 de Dic. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by