1
The data output from this exercise is not correct. I don’t know what I did wrong, if you can help me thank you very much
Create a program that reads 10 integers and stores them in a vector. After filling the vector, print only the even numbers of the vector:
#include <stdio.h>
int main ()
{
int veto [10];
int par [10];
int j = 0;
int n;
printf("Digite 10 numeros para identificar se sao pares ou nao\n");
for (int i = 0; i < 10; i++)
{
scanf("%i", &veto[i]);
if (veto[i] % 2 == 0)
{
par[j] = veto[i];
j++;
n = j;
}
}
for (; n > 0; n--)
{
printf("Os numeros pares sao %i", par[n]);
}
return 0;
}
Put an example of input and output. And the last loop should be n>=0 instead of n>0
– André Walker
Invert the commands
j++; n = j;
forn = j; j++;
, thusn
indicate the last element, andfor (; n > 0; n--)
utilizen >= 0
, because index 0 is the index of the first element of the vector.– anonimo