Skip to main content

Write a C program to create a file name DATA which will takes some integers from the terminal and read data from DATA file and will display odd numbers in ODD file and even numbers in EVEN file.



Answer:

#include<stdio.h>
#include<conio.h>
void main()
{
    FILE *f1,*f2,*f3;
    int number,i,a;
    clrscr();


    printf("Contents of what data: ");
    scanf("%d",&a);
    printf("\nContents of DATA file: ");
    f1 = fopen("DATA","w");
    for(i=1;i<=a;i++)
    {
            scanf("%d",&number);
            if(number==-1)break;
            putw(number,f1);
    }
    fclose(f1);
    f1 = fopen("DATA","r");
    f2 = fopen("ODD","w");
    f3 = fopen("EVEN","w");
    while((number = getw(f1)) != EOF)
    {
            if(number%2==0)
                putw(number,f3);
            else
                putw(number,f2);
    }
    fclose(f1);
    fclose(f2);
    fclose(f3);
    f2 = fopen("ODD","r");
    f3 = fopen("EVEN","r");
    printf("\nContents of ODD file: \n");
    while((number = getw(f2)) != EOF)
  printf("%4d",number);
    printf("\nContents of EVEN file: \n");
    while((number = getw(f3)) != EOF)
  printf("%4d",number);
    fclose(f2);
    fclose(f3);
    getch();
}

Simple Output:
Contents of what data: 5

Contents of DATA file: 1 2 3 4 5

Contents of ODD file:
            1          3          5

Contents of EVEN file:

            2          4

Popular posts from this blog

Draw the basic organization of computer

Answer:                 The basic organization of computer__ Input Unit: ·          It accepts (or reads) instructions and data from outside. ·          It converts these instructions and data in computer acceptable form ·          It supplies the converted instructions and data to the computer system for further processing. Central Processing Unit (CPU): Control Unit: Control unit of a computer system manages and coordinates the operations of all other components of the computer system. Arithmetic Logic Unit(ALU): Arithmetic logic unit of a computer system is the place, where the actual executions of instruction, takes place during processing operation. Storage Unit: Primary Memory: It is volatile ( loses data on power ...

Describe the four basic data types. How could we extend the range of values they represent?

Answer: The basic four data types are: Data Type Integer Type Character Type Floating Point Type Void Type signed int short int long int unsigned unsigned int unsigned short int unsigned long int char signed char unsigned char float double long double