Add prime numbers within a vector

3

I have this problem: Make an algorithm that receives 1500 numbers, compute and show the sum of the even numbers and the sum of the prime numbers.

So far so good, I have been able to filter and add even numbers, BUT, the sum of prime numbers always gives 0. I've created a separate algorithm to test if I can find prime numbers, it works:

#include <stdio.h>


int main()
{


    int numero, i, controle=0;


    printf("Digite um numero: ");


    scanf("%d", &numero);


    if (numero > 1)
    {

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


            if (numero % i == 0) 
            {
                controle = controle + 1;    
            }    
        }


        if (controle == 2)
        {
           printf("O numero %d e um numero primo!\n", numero);             
        }
        else
        {
            printf("O numero %d nao e um numero primo!\n", numero);    
        }

    }

}

Porém quando eu aplico ele no programa do exercício, ele não resulta em nada: 



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


main()

{
    int num[1500], i, par = 0, primo = 0, cont = 0;

    for(i=0; i < 5; i = i + 1)
    {
        printf("\n Digite um numero inteiro: ");
        scanf("%d", &num[i]);






    if(num[i]%2 == 0)
    {
     par = par + num[i];
    }

    }

    printf("\n A soma dos numeros pares: %d", par);


for(i=0; i < 5; i = i + 1)
    {
        if(num[i] > 1)
                {
                    for (i = 1; i <= num[i]; i =  i + 1)
                    {
                        if(num[i] % i == 0)
                        {
                            cont = cont + 1;    
                        }

                    }
                }
        if(cont == 2)
        {
            primo = primo + num[i];
            printf(" t ");
        }
    }
    printf("\n A soma dos numeros primos e: %d", primo);
    getch();
}
    
asked by anonymous 02.06.2016 / 18:10

2 answers

1

It's because you're using the same variable to control two for chained:

create a variable j and use it in the second for.

missing zero in the for too

Example:

int num[1500], i, j, par = 0, primo = 0, cont = 0;

for(i=0; i < 5; i = i + 1)
{
    printf("\n Digite um numero inteiro: ");
    scanf("%d", &num[i]);

    if(num[i]%2 == 0)
    {
     par = par + num[i];
    }
}

printf("\n A soma dos numeros pares: %d", par);


for(i=0; i < 5; i = i + 1)
{
    cont = 0
    if(num[i] > 1)
    {
        for (j = 1; j <= num[i]; j =  j + 1)
        {
            if(num[i] % j == 0)
            {
                cont = cont + 1;    
            }
        }
    }
    if(cont == 2)
    {
        primo = primo + num[i];
        printf(" t ");
    }
}
printf("\n A soma dos numeros primos e: %d", primo);
getch();
    
02.06.2016 / 18:32
1

Your code to check if the number is prime needs to be inside a function, where you pass the number of the main, and the routine returns whether it is a prime or not. Depending on the result, you add.

    
02.06.2016 / 18:29