How to test a number and tell whether it is prime or not?

1

I have a small problem, after the first time the code executes it only shows "NOT PRIME" and no longer shows "PRIME", where "NOT PRIME" would be for non prime numbers, and "PRIME" for numbers cousins

  #include <stdio.h>
  main()
  {

    int i, x=1; 
    int div = 0;

    while(x!=0)
    {
      do
      {
        printf("\n\t\tEntre com numero inteiro e positivo: \n\n");
        scanf("%d", &x);
      } while (x <= 0);   
      for (i = 1; i <= x; i++)
       {
        if (x % i == 0)
        { 
         div++;
        }
      }     
      if (div == 2)
      {
        printf("\t\t\nPRIME\n", x);
      }
      else
      {
        printf("\t\t\nNOT PRIME\n", x);
      }
    }
  }
    
asked by anonymous 11.03.2017 / 23:51

2 answers

1

Just set div to zero whenever while is completed, and secondly while changed condition to x < 0 since zero would be the number for the program to quit. >

#include <stdio.h>

int main(void) {

    int i, x; 
    int div;

    do {

        do {
            printf("\n\t\tEntre com numero inteiro e positivo: \n\n");
            scanf("%d", &x);
        } while (x < 0);   

        for (i = 1; i <= x; i++)
            if (x % i == 0)
                div++; 

        if(x != 0) {
            if (div == 2)
                printf("\t\t\nPRIME\n", x);
            else
                printf("\t\t\nNOT PRIME\n", x);
        } 

        div = 0;

    }while(x != 0);

    return 0;
}

No more corrected details such as identation, return, etc.

    
12.03.2017 / 00:15
1

You can use this function to check if the number is prime:

#include <stdio.h>

/* Retorna 1 para numeros primo ou 0 para numeros que nao sao primos.*/
int IsPrime(unsigned int number) {
    if (number <= 1) return 0; // se o numero for menor ou igual a 1 então nao é primo.
    unsigned int i;

    for (i = 2;  i * i <= number; i++) {
        if (number % i == 0) return 0;
    }

    return 1;
}

int main(void) {
    printf("0 é primo %s\n", IsPrime(0) ? "sim" : "nao");
    printf("1 é primo %s\n", IsPrime(1) ? "sim" : "nao");
    printf("3 é primo %s\n", IsPrime(3) ? "sim" : "nao");
    printf("2 é primo %s\n", IsPrime(2) ? "sim" : "nao");
    printf("4 é primo %s\n", IsPrime(4) ? "sim" : "nao");
    printf("5 é primo %s\n", IsPrime(5) ? "sim" : "nao");
    printf("7 é primo %s\n", IsPrime(7) ? "sim" : "nao");
    printf("9 é primo %s\n", IsPrime(9) ? "sim" : "nao");

    return 0;
}

Output:

  

0 is not prime cousin   1 is not a cousin
  3 is prime cousin
  2 is prime cousin
  4 is not a cousin
  5 is prime cousin
  7 is prime cousin
  9 is prime not

This IsPrime() function can be easily used in a loop, and will not have a high maintenance cost, see:

int i, v;

for(i = 0; i < 5; i++) {
    printf("Informe um numero: ");
    scanf("%i", &v);

    printf("%i é primo %s\n",v , IsPrime(v) ? "sim" : "nao");
}

It is not necessary to do the number verification calculation inside the same loop that reads the values entered by the user, it is always a good way to create separate functions to perform certain tasks, in this way you can reuse it and use it in other places.

And we also have to guarantee the treatment of the numbers entered by the user, so the use of unsigned for unsigned numbers, and the validation of the number at the beginning, however, it is necessary to make other validations of the data before sending to the IsPrime() function checks the number.

See working at Ideone .

Font .

    
12.03.2017 / 00:36