I have code below C that reads user input to the end of the file (ctrl + d) and stores them in an array. Then it should print all the odd numbers on the line, and then even the numbers on another line. For some reason, it does not work as expected.
When I enter the following:
1 2 4 16 32 64 128 256 512 1024 2048 4096 the output is: Odd numbers were: Even numbers were: 2 16 64 256 1024 4096 Expected output: Odd numbers were: 1 Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096
Code below:
#include <stdio.h> int main(void){ int array[1000]; int i,j,k; int counter = 0; for(i=0; scanf("%d", &array[i]) != EOF; i++){ scanf("%d", &array[i]); counter = counter+1; } printf("Odd numbers were: "); for(j=0; j<counter; j++){ if(array[j]%2 != 0){ printf("%d ", array[j]); } } printf("\n"); printf("Even numbers were: "); for(k=0; k<counter ; k++){ if(array[k]%2 == 0){ printf("%d ", array[k]); } } printf("\n"); }
source share