Code a C program to read five integer values from data.txt to console; calculate and display the sum and average thereof. Use the following data to create your text file: 6,45,12,67,9.
#include <stdio.h>
#define N 5
int main() {
int x, sum=0, i;
double avg;
FILE *fin;
fin = fopen("data.txt", "r");
for (i=0; i<N; i++) {
fscanf(fin, "%d,", &x);
printf("%d ", x);
sum += x;
}
fclose(fin);
printf("\nSum is %d,", sum);
avg = (double) sum / N;
printf(" avrage is %.2lf\n", avg);
return 0;
}
Comments
Leave a comment