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.
– Daniel Dutra