Code Competitions

Coding competition

Monday, 13 May 2013

Program to create a file of numbers and copy even numbers in one file and odd numbers in another file

#include<stdio.h>
#include<conio.h>
void main()
{
    FILE *fpd,*fpe,*fpo;
    int i;
    clrscr();

// Entering the numbers
    printf("\n\n Enter the number: \n");

// Opening the main file containing the numbers
    fpd=fopen("Data.txt","w");
    scanf("%d",&i);
    while(i!=-1)
    {
        putw(i,fpd);
        scanf("%d",&i);
    }
    fclose(fpd);

// Printing the numbers
    fpd=fopen("Data.txt","r");
    printf("\n\n");
    while((i=getw(fpd))!=EOF)
    {
        printf("%d\t",i);
    }

// Closing the main file
    fclose(fpd);

// Opening data,even and odd files
    fpd=fopen("Data.txt","r");
    fpe=fopen("even.txt","w");
    fpo=fopen("odd.txt","w");

// Separating even and odd numbers in odd and even files.
    while((i=getw(fpd))!=EOF)
    {
        if(i%2==0)
            putw(i,fpe);
        else
            putw(i,fpo);
    }
    fclose(fpd);
    fclose(fpe);
    fclose(fpo);

// Printing the even file.
    fpe=fopen("even.txt","r");
    printf("\n\n\n\n EVEN NUMBERS \n\n");
    while((i=getw(fpe))!=EOF)
    {
        printf("%d\t",i);
    }

// Printing the odd file.
    fpo=fopen("odd.txt","r");
    printf("\n\n\n\n ODD NUMBERS \n\n");
    while((i=getw(fpo))!=EOF)
    {
        printf("%d\t",i);
    }

    getch();
}

No comments:

Post a Comment