C Program to store odd and the even numbers entered in separate files..

#include<stdio.h>

void main()
{
    FILE *o,*e;
    // Writing to seperate files..
    o=fopen("odd.txt","w");
    e=fopen("even.txt","w");
    int n,a,i;
    printf("Enter the number of elements..");
    scanf("%d",&n);
    printf("\nEnter the elements one by one..");
    for(i=0;i<n;i++){
        scanf("%d",&a);
        if(a%2!=0)
            fprintf(o,"%d ",a);
        else
            fprintf(e,"%d ",a);
    }
    fclose(o);
    fclose(e);
    // Reading the even text file..
    printf("\nThe numbers in the even text file are.. ");
    char t;
    e=fopen("even.txt","r");
    while(!feof(e)){
        t=fgetc(e);
        printf("%c",t);
    }
    fclose(e);
   // Reading the odd text file..
    printf("\nThe numbers in the odd text file are.. ");
    o=fopen("odd.txt","r");
    while(!feof(o)){
        t=fgetc(o);
        printf("%c",t);
    }
    fclose(o);
}

No comments:

Post a comment