-
Notifications
You must be signed in to change notification settings - Fork 1
/
evenoroddfile.c
53 lines (42 loc) · 1.19 KB
/
evenoroddfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
int main()
{
FILE *inputFile, *evenFile, *oddFile;
int num;
// Open the input file in read-mode
inputFile = fopen("input.txt", "r");
if (inputFile == NULL) {
printf("Unable to open input file.\n");
return 1;
}
// Open the even file in write-mode
evenFile = fopen("even.txt", "w");
if (evenFile == NULL) {
printf("Unable to open even file.\n");
return 1;
}
// Open the odd file in write-mode
oddFile = fopen("odd.txt", "w");
if (oddFile == NULL) {
printf("Unable to open odd file.\n");
return 1;
}
// Read each number from the input file
while (fscanf(inputFile, "%d", &num) == 1)
{
// Check if the number is even or odd
if (num % 2 == 0) {
// Write even number to even file
fprintf(evenFile, "%d\n", num);
} else {
// Write odd number to odd file
fprintf(oddFile, "%d\n", num);
}
}
printf("Numbers separated successfully.\n");
// Close the files
fclose(inputFile);
fclose(evenFile);
fclose(oddFile);
return 0;
}