C program to store even, odd and prime numbers into separate files

In C, we can read numbers from a file and categorize them into even, odd, and prime numbers, storing each category in separate files. This involves file I/O operations and number classification algorithms.

Syntax

FILE *fopen(const char *filename, const char *mode);
int fscanf(FILE *stream, const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int fclose(FILE *stream);

Note: This program requires creating an input file named "numbers.txt" with integers to process. Since file operations cannot be executed in the online compiler, the code structure is shown for educational purposes.

Example

The following program reads integers from an input file and separates them into three categories −

#include <stdio.h>
#include <stdlib.h>

/* Function declarations */
int even(const int num);
int prime(const int num);

int main(){
    FILE * fptrinput,
         * fptreven,
         * fptrodd,
         * fptrprime;
    int num;
    
    /* Open files */
    fptrinput = fopen("numbers.txt", "r");
    fptreven = fopen("even-numbers.txt" , "w");
    fptrodd = fopen("odd-numbers.txt" , "w");
    fptrprime= fopen("prime-numbers.txt", "w");
    
    /* Check if files opened successfully */
    if(fptrinput == NULL || fptreven == NULL || fptrodd == NULL || fptrprime == NULL){
        printf("Unable to open file.
"); exit(EXIT_FAILURE); } printf("File opened successfully. Reading integers from file.

"); /* Read integers and categorize */ while (fscanf(fptrinput, "%d", &num) == 1){ if (prime(num)) fprintf(fptrprime, "%d
", num); else if (even(num)) fprintf(fptreven, "%d
", num); else fprintf(fptrodd, "%d
", num); } /* Close all files */ fclose(fptrinput); fclose(fptreven); fclose(fptrodd); fclose(fptrprime); printf("Data written successfully."); return 0; } /* Function to check if number is even */ int even(const int num){ return !(num & 1); } /* Function to check if number is prime */ int prime(const int num){ int i; if (num <= 1) return 0; if (num == 2) return 1; if (num % 2 == 0) return 0; for (i = 3; i * i <= num; i += 2) { if (num % i == 0) { return 0; } } return 1; }

Expected Output

File opened successfully. Reading integers from file.

Data written successfully.

File Contents Example

Assuming the input file numbers.txt contains: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

After execution, the output files will contain:

File Name Contents Description
prime-numbers.txt 2 3 5 7 11 13 17 Prime numbers only
even-numbers.txt 4 6 8 10 12 14 16 Even non-prime numbers
odd-numbers.txt 1 9 15 Odd non-prime numbers

Key Points

  • Prime numbers take priority over even/odd classification
  • The program uses optimized prime checking with early termination
  • File error handling prevents crashes if files cannot be opened
  • All files must be properly closed to avoid data loss

Conclusion

This program demonstrates file I/O operations combined with mathematical algorithms to categorize numbers. It efficiently separates integers into prime, even, and odd categories using separate output files.

Updated on: 2026-03-15T14:10:44+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements