C language-prime numbers in vectors

I'm solving exercises, in C language, about vectors. The exercise is as follows:

Make a program that loads a vector of 10 integers, shows only the prime numbers and their respective positions.

Right I solved the exercise, only my code is only checking the first number.

 #include <stdio.h>

 #define VET 10

//Convenção verifica = 1 --> O numero é primo

//          verifica = 0 --> O numero não é primo
main()
{
    int n[VET],i;
    int d; // divisor
    int verifica; // verifica se o numero é primo.

    d=2;
    verifica=1;

    for(i=0; i<10 ;i++)
    {
        printf("\nDigite um numero:");
        scanf("%d",&n[i]);
        printf("O numero digitado foi: %d\n",n[i]);

        if (n[i] <= 1)
        verifica = 0;

            while(verifica == 1 && d <= n[i] / 2) 
             {
                if (n[i] % d  == 0)
                verifica = 0;
                d = d + 1;
             }

        if (verifica == 1)
        printf("%d eh primo.Sua posicao eh %d.\n", n[i],i);

    }

    return 0;   
    system("pause");
}

Obs1: I used a code from someone else, just to check if the number is prime or not.

 4
Author: Mansueli, 2014-07-09

4 answers

You have generated an infinite loop and so you cannot continue to check the next numbers.

#include <stdio.h>
#include <locale.h> //para usar acentuação em português
#include <math.h> // utilizar a função de raiz quadrada (sqrt)
#define TAMANHO 10
//Convenção verifica = 1 --> O numero é primo
//          verifica = 0 --> O numero não é primo
main()
{
    setlocale(LC_ALL, "Portuguese");
    int n[TAMANHO] = {13,24,15,22,11,
                  19,18,23,25,47,};
    int d,i,limite;
    int verifica; // bandeira indicativa de verificação de numero primo.


    verifica=1;

    for(i=0; i<TAMANHO ;i++)
    {
        if (n[i] > 1) { //só verifica se o número for maior que 1
            d = 2;
            verifica = 1;
            limite = sqrt(n[i]); // determina o limite de busca de dividendos até a raiz quadrada do número analisado
            while(verifica && d <= limite) //laço de verificação
             {
               //se o número for divisível por d, este não é primo
                if (n[i] % d  == 0){ 
                    //define como não primo
                    verifica = 0;
                }
                //incrementa o número para testar
                d++;
             } 
            // imprime se primo
            if (verifica) // é o mesmo que verifica == 1
                printf("O número %d, na posição: n[%d] é primo.\n", n[i],i);
        }
    }
    return 0;   
}
 2
Author: Mansueli, 2014-07-31 17:16:07

You weren't resetting the values of d and verifica with each iteration of for, so your d kept growing, and it only worked out the first time. And also, when I identified a non-prime number, from there verifica would be zeroed in on the next iterations.

Then do this:

Cut lines:

  d=2;
  verifica=1;

And paste right at the beginning of for, which will work:

insert the description of the image here

 0
Author: FLemos, 2014-07-31 16:47:54

I set up based on the above program, a program in which the user tells how many numbers, it will provide and at the end shows the amount of numbers provided and how many are primes and which are primes.

#include <stdio.h> < stdio.h>
#include <locale.h> < locale.h>
#include <math.h> < math.h>

main()
{

    setlocale(LC_ALL, "Portuguese");

    int d,i,k,cont,defin;
    int teste;


    teste=1;
    cont=0;

    printf("**********************************************\n");
    printf("Informe quantos números serão informados:");
    scanf("%i", &k);
    printf("\n**********************************************");
    printf("\n\n\n");

    int n[k];

    for(i=0; i<k ;i++)
    {
        printf("\n");
        printf("Digite um numero:");
        scanf("%d",&n[i]);
        printf("O numero digitado foi: %d\n",n[i]);
        if (n[i] > 1) {
            d = 2;
            teste = 1;
            defin = sqrt(n[i]); 
            while(teste && d <= defin)
             {
                if (n[i] % d  == 0){ 
                    teste = 0;
                }
                d++;
             }
            if (teste==1){
                cont+=1;
                printf("O número %d é primo.\n", n[i]);
                printf("\n");
                        }
            else{
                printf("O número %d não é primo.\n", n[i]);
                printf("\n");
                }
        }
        else{
            if(n[i]==1)
                        {
                        printf("O número %d não é primo.\n", n[i]);
                        printf("\n");
                        }
            else
                {
                printf("O número %d não é positivo.\n", n[i]);
                printf("\n");
                }
            }
        }

    printf("\n\n\n");
    printf("**********************************************\n");
    printf("**********************************************\n");
    printf("CONCLUSÃO, De %i números digitados, %i são primos...\n",k,cont);
    printf("**********************************************\n");
    printf("**********************************************\n\n");

    for(i=0; i<k ;i++)
    {
        if (n[i] > 1) {
            d = 2;
            teste = 1;
            defin = sqrt(n[i]);
            while(teste && d <= defin)
             {
                if (n[i] % d  == 0){ 
                    teste = 0;
                }
                d++;
             }
            if (teste==1)
                printf("O número %d é primo.\n", n[i]);

                        }
    }
    printf("\n**********************************************\n");
    printf("**********************************************\n");
    printf("\n\n\n");
    system("pause");
    return 0;   
}
 0
Author: Erich Wedemann, 2014-11-26 09:09:21

/ / simple and precise

#include <stdlib.h>
#include <stdio.h>

int main() { 
    int usuario[10],i;

    for (i=1;i<=9;i++) {
        do  {

            printf("digite o %d numero maior que um e que seja positivo.: ",i);
            scanf("%d",&usuario[i]);
        }
        while(usuario[i]<=1);
    }

    for (i=1;i<=9;i++) {

        if(usuario[i]==2) {
            printf("\nnumero %d sim e primo e esta na posicao %d do vetor\n",usuario[i],i);
        } else  if(usuario[i] %2==0) {
            printf("\nnumero %d nao e primo e esta na posicao %d do vetor\n",usuario[i],i);
        } else
            printf("\nnumero %d sim e primo e esta na posicao %d do vetor\n",usuario[i],i);
    }
}
 0
Author: FERNANDO ORIAS BRANCO, 2018-10-15 07:19:27