Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. ´´´c
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. int esprimo(int n){
  7.  
  8. int p=1;
  9.  
  10. if(n<=0){
  11. p=-1;
  12. return n;
  13. }
  14.  
  15. int i,div=0; //contador para la cantidad de divisores
  16. for( i=1; i<=n; i++ ){
  17.  
  18. if( n%i==0 ){
  19. div++; //solo deberia sumar si la division tiene resto 0
  20. }
  21. if( div>2 ){
  22. p=0;
  23. }
  24. }
  25. return p;
  26. }
  27.  
  28. int main(){
  29.  
  30. int n,p;
  31. printf("Ingrese un numero positivo: ");
  32. scanf("i%", &n);
  33.  
  34. p = esprimo(n);
  35.  
  36. if(p==0){
  37. printf("No es primo");
  38. }
  39. else if(p==1){
  40. printf("Es primo");
  41. }
  42. else if(p==-1){
  43. printf("Numero no valido");
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49. ´´´
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement