Add prime numbers within a vector

Asked

Viewed 895 times

3

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

So far so good, I was able to filter and add even numbers, BUT the sum of prime numbers always gives 0. I 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();
}
  • Congratulations on reading the rules before posting!! I recommend taking the first sentence out of the question, that kind of thing I usually put in the comment(even if it is from my own question). You forgot to put the tag of the language you are using, this helps a lot so that your question has more access, and therefore is answered faster.

2 answers

1

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

1


is because you are using the same variable to control two is chained:

create a j variable and use in the second for.

failed to reset the cont inside the is also

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();
  • Got the idea, thanks , but if I reset the count when declaring, I need to reset it again? I don’t understand why.

  • Yes, when you Zera in the declaration the cont starts with value equal to zero, but image that the first number informed is prime, when processing the second number, cont will continue with value equal to 2, so you need to reset again

  • In this case with a simple table test you would already find the problem, recommend you learn more about table test

  • Yes, I need to give more value to the table test, I will pay attention to it. Thanks Rodrigo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.