0
I have the following problem: I need to read several numbers until the number typed is 0 or one of the vectors is fully filled, in case the vector must have a maximum of 10 indexes, and for each typed number must-separate the odd pairs on each vector respectively. But when one of the vectors is filled the program does not finish, it repeats the question of 'for' successively ending only when the value 0 is entered.
#include <stdio.h>
main(){
int par[10], impar[10], i, num, pares = 0, impares = 0;
do{
printf("\n Digite um numero: ");
scanf("%d", &num);
for ( i=0; i < 20; i++){
if (num % 2 == 0){
par[i] = num;
pares ++;
}
else if (num % 2 != 0){
impar[i] = num;
impares++;
}
}
}while((num != 0) && (pares < 10 ) || (impares < 10));
for (i=0; i<10; i++){
printf("\n Pares: %d", par[i]);
printf("\n Impares: %d", impar[i]);
}
}
Change
(pares < 10 ) || (impares < 10)
for(pares < 10) && (impares < 10)
. Problem solved?– Costamilam
To avoid confusion with the condition
while
I would make an infinite cycle withbreak
inside associated with very simple conditions:if (num == 0) break;
,if (pares == 10) break;
– pmg
in-house?
– João Vitor Lumertz