Write a C program to find the total and average mark of a student using arrays.
#include <stdio.h>
#define MAXLIMIT 256
int main() {
int mark[MAXLIMIT], i, n, total = 0;
float average;
/* get the number of subjects from the user */
printf("Enter the number of subjects:");
scanf("%d", &n);
/* get the marks for each subject from user */
printf("Enter your marks:\n");
for (i = 0; i < n; i++) {
printf("Subject[%d]: ", i);
scanf("%d", &mark[i]);
}
/* calculate the total */
for (i = 0; i < n; i++) {
total = total + mark[i];
}
/* find the average */
average = (1.0 * total) / n;
/* print the total and average */
printf("Total: %d\n", total);
printf("Average: %.3f\n", average);
return 0;
}
#define MAXLIMIT 256
int main() {
int mark[MAXLIMIT], i, n, total = 0;
float average;
/* get the number of subjects from the user */
printf("Enter the number of subjects:");
scanf("%d", &n);
/* get the marks for each subject from user */
printf("Enter your marks:\n");
for (i = 0; i < n; i++) {
printf("Subject[%d]: ", i);
scanf("%d", &mark[i]);
}
/* calculate the total */
for (i = 0; i < n; i++) {
total = total + mark[i];
}
/* find the average */
average = (1.0 * total) / n;
/* print the total and average */
printf("Total: %d\n", total);
printf("Average: %.3f\n", average);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the number of subjects:5
Enter your marks:
Subject[0]: 95
Subject[1]: 96
Subject[2]: 97
Subject[3]: 98
Subject[4]: 99
Total: 485
Average: 97.000
Enter the number of subjects:5
Enter your marks:
Subject[0]: 95
Subject[1]: 96
Subject[2]: 97
Subject[3]: 98
Subject[4]: 99
Total: 485
Average: 97.000
No comments:
Post a Comment