program that writes the even and odd numbers
Posted
by user292489
on Stack Overflow
See other posts from Stack Overflow
or by user292489
Published on 2010-03-29T21:47:54Z
Indexed on
2010/03/29
21:53 UTC
Read the original article
Hit count: 402
c
enter code here
i was writting a program that can read a set of numbers file called dog.txt;
and also writes to two file separating odd and even. i was able to compile my program however, the output expected is not the same which was supposed to be even numbers in one file called EVEN, odd numbers in file odd.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
int even,odd;
int num;
if (argc != 4) {
printf("Usage: executable in_file output_file\n");
exit(0);
}
FILE *dog = fopen(argv[1], "r");
FILE *feven= fopen(argv[2], "w");
FILE *fodd= fopen (argv[3], "w");
while (fscanf(dog, "%d", &num) != EOF)
{
if (0==i%2){
i++;
printf("even= %d\n", num);
}
else if(i!=0){
i++;
printf("odd= %d\n", num);
}
}
fclose(feven);
fclose(fodd);
fclose(dog);
return 0;
}
output:
even= 1
odd= 2
even= 34
odd= 44
even= 66
odd= 78
even= 94
odd= 21
even= 23
odd= 54
even= 44
odd= 65
even= 78
odd= 68
even= 92
© Stack Overflow or respective owner