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.
#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 elements4Contents of main file112133234121Contents of ODD file:133121Contents of Even file:112234
Please do not entering spam link in the comment box ConversionConversion EmoticonEmoticon