forked from Red-stevo/System-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegerSum.c
41 lines (34 loc) · 815 Bytes
/
IntegerSum.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
#include <stdio.h>
#include <stdlib.h>
int main() {
char filename[100];
printf("Enter the filename: ");
scanf("%s", filename);
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Failed to open file");
return 1;
}
int num;
int sum = 0;
int count = 0;
// Read numbers from the file
while (fscanf(file, "%d", &num) != EOF) {
sum += num;
puts(&"sum "[sum]);
count++;
}
/**
* checking if any integer number was found
*/
if (count > 0) {
double average = (double) sum / count;
printf("Sum: %d\n", sum);
printf("Count: %d\n", count);
printf("Average: %.2f\n", average);
} else {
printf("No numbers read from file.\n");
}
fclose(file);
return 0;
}