In this post, we are going to see how to write a C program to find Student Total marks & Percentages.
What will you learn in this C program?:
- How to get the number of subjects dynamically.
- You will learn how to set a dynamic array variable in C.
- Get the size of an array in C.
- How to find the percentage in C language.
#include <stdio.h>
int main() {
//set the integers
int subj, i, mark;
float total = 0, percentage;
printf("No. of Subjects?: ");
scanf("%d", &subj);
//dynamic array
int marks[subj];
//get the array size
int arr_size = sizeof(marks) / sizeof(marks[0]);
//for loop to get the marks & calculate them
for(i=0; i < arr_size; i++) {
printf("Subject %d:", i+1);
scanf("%d", &mark);
total = total + mark;
}
//let's find the percentage
percentage = ( total / arr_size * 100) / 100;
//print the result
printf("Total marks = %f\n", total);
printf("Percentage = %f\n", percentage);
return 0;
}Output:
No. of Subjects?: 6 Subject 1:98 Subject 2:95 Subject 3:85 Subject 4:79 Subject 5:81 Subject 6:80 Total marks = 518.000000 Percentage = 86.333344 -------------------------------- Process exited after 23.24 seconds with return value 0 Press any key to continue . . .
Read the inline comments to understand each section of the program. I believe this post would have helped you to understand how to write a C program to find Student marks.