C Program to Find Total of Even Integers

C Program to Write ODD, and EVEN Numbers Integer Data Files

To Write C program that would print ODD and EVEN numbers handling Integer Data files. The C program to give step by step simple example.The file named DATA contains series of integer numbers. Code a program to read these numbers and then write all ‘odd’ numbers to a file to be called ODD and all `even’ numbers to a file to be called EVEN.

The program is shown below example. It uses three files simultaneously and therefore we need to define three-file pointers f1, f2 and f3.

First, the file DATA containing integer values is created. The integer values are read from the terminal and are written to the file DATA with the help of the statement putw(number, f1);

Notice that when we type -1, the reading is terminated and the file is closed. The next step is to open all the three files, DATA for reading, ODD and EVEN for writing. The contents of DATA file are read, integer by integer, by the function getw(f1) and written to ODD or EVEN file after an appropriate test. Note that the statement

(number = getw(f1)) != EOF

reads a value, assigns the same to number, and then tests for the end-of-file mark.


Finally, the program displays the contents of ODD and EVEN files. It is important to note that the files ODD and EVEN opened for writing are closed before they are reopened for reading.

Example : C Program to Write ODD and EVEN Numbers

#include <stdio.h>
main()
{
   FILE *f1, *f2, *f3;
   int number, i;
   printf("Contents of DATA file\n\n");
   f1 = fopen("DATA", "w"); /* Create DATA file */
   for(i = 1; i <= 30; 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");

/* Read from DATA file */
while((number = getw(f1)) != EOF)
{
    if(number %2 == 0)
       putw(number, f3); /* Write to EVEN file */
    else
       putw(number, f2); /* Write to ODD file */
}
fclose(f1);
fclose(f2);
fclose(f3);

f2 = fopen("ODD","r");
f3 = fopen("EVEN", "r");
printf("\n\nContents of ODD file\n\n");

while((number = getw(f2)) != EOF)
     printf("%4d", number);
printf("\n\nContents of EVEN file\n\n");

while((number = getw(f3)) != EOF)
     printf("%4d", number);
fclose(f2);
fclose(f3);
}

Output

Contents of DATA file
111 222 333 444 555 666 777 888 999 000 121 232 343 454 565 -1
Contents of ODD file
111 333 555 777 999 121 343 565
Contents of EVEN file
222 444 666 888 0 232 454

Let me know if you find any difficulty in understanding this C Program write ODD and EVEN numbers handling Integer Data files. example and I would be glad to explain it further.

Online Training Tutorials

  • C Programming Examples – Simple C ProgramsC Programming Examples – Simple C Program for beginnersIn this article, we will share you, C Programming Examples on different topic like, Loops, functions, pointers, mathematical calculation, searching algorithms, and structures and many of […]
  • C Program to Print Prime NumbersC Program to Print Prime Numbers up to Given NumberFollowing C program Print Prime Numbers up to Given Number, then display the result on the screen: A for loop is a repetition control structure that allows you to efficiently write a […]
  • Display Fibonacci SequenceC Program to Display Fibonacci SequenceTo Write a program that would to Display Fibonacci Sequence. The C program to display Fibonacci Sequence using loops is given in below simple example. Unlike for and while loops, which […]
  • C Program to Find Total of Even IntegersC Program to Find Total of Even IntegersThe following C Program to Find Total of Even Integers and display them in ascending alphabetical order. An array is a collection of variables of the same type. All arrays consist of […]
  • Print Product of Two MatricesC Program to Print Product of Two MatricesFollowing C Program to Print Product of Two Matrices, then display the result on the screen: The simplest form of multidimensional array is the two-dimensional array. A two-dimensional […]
  • C Program to Find Total of Even IntegersC Program to Find Factorial of Number Using RecursionTo Write C program that would find factorial of number using Recursion. The function is a group of statements that together perform a task. Every C program has at least one function, which […]
  • C Program to Find Total of Even IntegersC Program to Sort set of strings in Alphabetical OrderThe following program key user to be ask to enter set of Strings and the program would sort and display them in ascending alphabetical order. To Write a program that would sort list of […]
  • C Program to Find Total of Even IntegersString Handling Function in C ProgrammingC program provides set of pre-defined functions called string handling functions to work with string values in programming. The string handling functions are defined in a header file […]