- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to store even, odd and prime numbers into separate files
A file is a physical storage location on disk and a directory is a logical path which is used to organise the files. A file exists within a directory.
The three operations that we can perform on file are as follows −
- Open a file.
- Process file (read, write, modify).
- Save and close file.
Program
Following is the C program to store even, odd and prime numbers into separate files −
#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, success; fptrinput = fopen("numbers.txt", "r"); fptreven = fopen("even-numbers.txt" , "w"); fptrodd = fopen("odd-numbers.txt" , "w"); fptrprime= fopen("prime-numbers.txt", "w"); if(fptrinput == NULL || fptreven == NULL || fptrodd == NULL || fptrprime == NULL){ /* Unable to open file hence exit */ printf("Unable to open file.
"); exit(EXIT_FAILURE); } /* File open success message */ printf("File opened successfully. Reading integers from file.
"); // Read an integer and store read status in success. 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); } fclose(fptrinput); fclose(fptreven); fclose(fptrodd); fclose(fptrprime); printf("Data written successfully."); return 0; } int even(const int num){ return !(num & 1); } int prime(const int num){ int i; if (num < 0) return 0; for ( i=2; i<=num/2; i++ ) { if (num % i == 0) { return 0; } } return 1; }
Output
When the above program is executed, it produces the following result −
File opened successfully. Reading integers from file. Data written successfully.
Explanation
Given below is an explanation for the program used to store even, odd and prime numbers into separate files −
Input file: numbers.txt file contains: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Which is open in read mode (already exists file) Separated even, odd and prime numbers in separate file after execution even-numbers.txt contains: 4 6 8 10 12 14 16 odd-numbers.txt contains: 9 15 prime-numbers.txt contains: 1 2 3 5 7 11 13 17
- Related Articles
- The sum of two prime numbers $(>2)$ is(A) odd (B) even (C) prime (D) even or odd
- Separate odd and even in JavaScript
- Even numbers at even index and odd numbers at odd index in C++
- C# program to split the Even and Odd integers into different arrays
- How to separate even and odd numbers in an array by using for loop in C language?
- State whether the following statements are True or False:(a) The sum of three odd numbers is even.(b) The sum of two odd numbers and one even number is even.(c) The product of three odd numbers is odd.(d) If an even number is divided by 2, the quotient is always odd.(e) All prime numbers are odd.(f) Prime numbers do not have any factors.(g) Sum of two prime numbers is always even.(h) 2 is the only even prime number.(i) All even numbers are composite numbers.(j) The product of two even numbers is always even.
- What are prime numbers and composite numbers And how we can know thank the prime or composite numbers are odd or even?
- Python program to Count Even and Odd numbers in a List
- Swift Program to Get Odd and Even Numbers From the Array
- What are even and odd numbers?
- Largest Even and Odd N-digit numbers in C++
- Python program to split the even and odd elements into two different lists.
- Java program to split the Even and Odd elements into two different lists
- Write a Golang program to find odd and even numbers using bit operation
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++

Advertisements