---
title: "Odd and even numbers to separate files"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-odd-even-until-no"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-odd-even-until-no
site: BCT Engineering
---

# Odd and even numbers to separate files

Computer Programming · Chapter 10: Data Files

- Asked in: 2074 Asoj, 4 marks; 2069 Chaitra, 8 marks (asked 2 times)
- Video: [program to read 1-100 integers and store odd and even numbers into separate file](https://www.youtube.com/watch?v=5ca_RJHXpSc) — Learn by Doing
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-odd-even-until-no
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Write a program in C to read integers from user until user says "no". After reading the data write all the odd numbers to a file named odd.txt and all the even numbers to file named even.txt. (Take care of all possible error conditions to open a new file named DATA.)

## Short answer

Read integers until user says 'no', writing all to DATA file. Then reopen DATA for reading, and create odd.txt and even.txt. Read each integer, write to odd.txt if odd, else even.txt. Handle file open errors and invalid input.

## Answer

**Logic.**
1. Create the file `DATA` in write mode; if `fopen()` returns `NULL`, print an error and stop.
2. Read integers in a loop, write each into `DATA`, and ask "Continue (yes/no)?" until the user types `no`. Invalid input stops the loop.
3. Close `DATA`, open it again for reading and open `odd.txt` and `even.txt` for writing, checking every `fopen()` for `NULL`.
4. Read each number from `DATA` with `fscanf()`; write it to `odd.txt` if `x % 2 != 0`, otherwise to `even.txt`.
5. Close all files.

Error conditions handled: failure of `fopen()` for each file, and input that is not an integer.

```c
#include <stdio.h>
#include <string.h>

int main(void)
{
    FILE *fd, *fo, *fe;
    int n, x;
    char ans[10];

    fd = fopen("DATA", "w");              /* new file DATA for all numbers */
    if (fd == NULL) {
        printf("Cannot create DATA\n");
        return 1;
    }
    do {
        printf("Enter an integer: ");
        if (scanf("%d", &n) != 1) {       /* invalid input */
            printf("Invalid input\n");
            break;
        }
        fprintf(fd, "%d\n", n);
        printf("Continue (yes/no)? ");
        scanf("%9s", ans);
    } while (strcmp(ans, "no") != 0);
    fclose(fd);

    fd = fopen("DATA", "r");
    fo = fopen("odd.txt", "w");
    fe = fopen("even.txt", "w");
    if (fd == NULL || fo == NULL || fe == NULL) {
        printf("File opening error\n");
        return 1;
    }
    while (fscanf(fd, "%d", &x) == 1) {
        if (x % 2 != 0)
            fprintf(fo, "%d\n", x);
        else
            fprintf(fe, "%d\n", x);
    }
    fclose(fd);
    fclose(fo);
    fclose(fe);
    printf("Done. See odd.txt and even.txt\n");
    return 0;
}
```

**Sample run** (numbers 7, 12, 5, 8, 21)

Input: `7 yes 12 yes 5 yes 8 yes 21 no`

```text
Enter an integer: Continue (yes/no)? Enter an integer: Continue (yes/no)? Enter an integer: Continue (yes/no)? Enter an integer: Continue (yes/no)? Enter an integer: Continue (yes/no)? Done. See odd.txt and even.txt
```

odd.txt contains 7, 5, 21 and even.txt contains 12, 8.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the purpose of writing all numbers to DATA first?
   A. It holds all entered numbers so they can later be read back and split into odd and even files
   B. It sorts the numbers
   C. It doubles the numbers
   D. It checks for errors only
   Answer: A. DATA is closed, reopened for reading, and each number is distributed to odd.txt or even.txt. (Key point: Why DATA first)

2. Which condition sends a number x to odd.txt?
   A. x % 2 == 0
   B. x % 2 != 0
   C. x / 2 != 0
   D. x & 2
   Answer: B. An odd number leaves a non-zero remainder when divided by 2. (Key point: Odd test)

3. Which error conditions does the program handle?
   A. Only invalid input
   B. fopen() failure for each file and non-integer input
   C. Only disk full
   D. None
   Answer: B. Each fopen() is checked for NULL and scanf's return value is checked for invalid input. (Key point: Error conditions)

4. How does the program stop reading numbers from DATA?
   A. while (fscanf(fd, "%d", &x) == 1)
   B. After exactly 10 numbers
   C. When x is 0
   D. When the user types no
   Answer: A. fscanf returns 1 while a number is read and a different value at the end of the file. (Key point: Reading the file back)

## Related in Computer Programming

- [Count even and odd until -1](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acount-even-odd-until-minus1) — Computer Programming, chapter 5
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [RECORD.txt item cost with VAT](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-record-txt-invoice) — Computer Programming, chapter 10
- [Copy contents of one file to another](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-copy-contents) — Computer Programming, chapter 10, asked 2×
- [I/O functions used with data files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-functions) — Computer Programming, chapter 10, asked 2×
- [Count prime and composite until -1](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acount-prime-composite-until-minus1) — Computer Programming, chapter 5

## Related in other subjects

- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
- [Even and odd parts of a signal](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Aeven-odd-parts) — Data Communication, chapter 2
- [8086 ALP: sum of 100 even numbers](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-sum-even-numbers) — Microprocessor, chapter 3
