I wrote a program to print odd and even numbers in separate files. My program is
#include<stdio.h>
int main()
{
FILE *f1,*f2,*f3;
int n,i,num;
f1 = fopen("number.txt","w");
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
fprintf(f1,"%d ",i);
fprintf(f1,"\n");
fclose(f1);
f1 = fopen("number.txt","r");
f2 = fopen("even.txt","w");
f3 = fopen("odd.txt","w");
fprintf(f2,"Even numbers:\n");
fprintf(f3,"Odd numbers:\n");
while(!feof(f1)){
fscanf(f1,"%d",&num);
if(num%2 == 0)
fprintf(f2,"%d ",num);
else
fprintf(f3,"%d ",num);
}
fclose(f1);
fclose(f2);
fclose(f3);
return 0;
}
And the output is
Enter the number:10
$ cat number.txt
1 2 3 4 5 6 7 8 9 10
$ cat even.txt
Even numbers:
2 4 6 8 10 10
$ cat odd.txt
Odd numbers:
1 3 5 7 9
Why am I getting two 10s in the even output?