Monday, November 26, 2018

C code review seperates odd-even numbers & store them in seperate data files

This C code shows how to seperates odd-even numbers & store them in seperate data files. And code is helpful while user is in between digit-sorting manipulation task.

code:

//Tested Ok on Code::Block IDE 17.12 & Cygwin+GCC tools.
//This program takes any number of integers from the input terminal
//writes them into a file and then writes the even and the odd integers
//into separate file.

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

    printf("Enter %d integers: \n", 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");
    printf("\nTHE EVEN NUMBERS ARE");
    while((number=getw(even))!=EOF)
        printf(" %4d",number);
    printf("\nTHE ODD NUMBERS ARE");
    while((number=getw(odd))!=EOF)
        printf("  %4d",number);
    printf("\nCheck source code folder for three data files.");
    fclose(even);
    fclose(odd);
}

//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment