Program to print Even and Odd numbers from the file in c language | C programs for practice

Program to print Even and Odd numbers from the file

In this post we are learning a Program to print Even and Odd numbers from the file in c language.

Program to print Even and Odd numbers from the file in c language



#include <stdio.h> 
#include <conio.h>
main ()
FILE *fl,*f2,*f3; 
int n,i,a,m; 
printf ("\n Enter the Number of elements:");
scanf("%d",&m); 
printf("\n Contents of main file"); 
fl=fopen ("Main","w"); 
for (i=1;i<=m;i++) 
scanf("%d",&n);
if (n==-1)
break; 
putw (n,fl); 
fclose (fl);
fl=fopen ("Main","r");
f2=fopen ("ODD","w"); 
f3=fopen ("EVEN","w"); 
a=getw (fl); 
while (a!=EOF) 
if (a%2==0) 
putw (a,f3); 
else 
putw (a,f2); 
a=getw(fl);
fclose (fl); 
fclose (f2); 
e file. fclose (f3); 
f2=fopen ("ODD","r");
f3=fopen ("EVEN","r"); 
printf("\n Contents of ODD file: \n"); 
while ((a=getw(f2))!= EOF)
{
printf("%d\n",a);
printf("\n Contents of Even file: \n"); 
while ((a=getw(f3)!=EOF)
{
printf("%d\n",a);
}
fclose(f2); 
fclose(f3);
getch ();
}

Output:

Enter the Number of elements 
4
Contents of main file 
112 
133 
234 
121 
Contents of ODD file: 
133 
121 
Contents of Even file: 
112 
234
Previous
Next Post »

Please do not entering spam link in the comment box ConversionConversion EmoticonEmoticon