World's most popular travel blog for travel bloggers.

: Write a C program to take a list of N numbers, separate even and odd numbers and put them in two appropriate files (evenfile and oddfile). Use File Handling concept.

, , 14 comments

Ans.
#include<stdio.h>
#include<math.h>
void main()
{
FILE *all,*even,*odd;
int number,i,records;
scanf("INPUT THE TOTAL NUMBER OF RECORDS THAT U WANT TO ENTER

");
scanf("%d",& records);
all=fopen("ANYNUMBER","w");
for(i=1;i<=records;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,all);
}
fclose(all);
all=fopen("ANYNUMBER","r");
even=fopen("EVENNUMBER","w");
odd=fopen("ODDNUMBER","w");
while((number=getw(all))!=EOF)
{
if(number%2==0)
putw(number,even);
else
putw(number,odd);
}
fclose(all);
fclose(even);
fclose(odd);
even=fopen("EVENNUMBER","r");
odd=fopen("ODDNUMBER","r");
scanf("THE EVEN NUMBERS ARE");
while((number=getw(even))!=EOF)
scanf(" %4d",number);
scanf("THE ODD NUMBERS ARE");
while((number=getw(odd))!=EOF)
scanf("  %4d",number);
fclose(even);
fclose(odd);
}

14 comments:

Let us know your responses and feedback