w3resource

C Exercises: Calculate the average marks of mathematics of some students

C Basic Declarations and Expressions: Exercise-44 with Solution

Write a C program to calculate the average mathematics marks of some students. Input 0 (excluding to calculate the average) or a negative value to terminate the input process.

Pictorial Presentation:

C Programming: Calculate the average marks of mathematics of some students

Sample Solution:

C Code:

#include <stdio.h>
int main() {
    int marks[99], m, i, a=0, total=0;
    float f;
    
    // Prompt for user input
    printf("Input Mathematics marks (0 to terminate): ");
    
    // Loop to get input marks
    for(i = 0; ; i++) {
        scanf("%d", &marks[i]);
        if(marks[i] <= 0) {
            break;
        }
        a++;
        total += marks[i];
    }
    
    // Calculate and print average
    f = (float)total/(float)a;
    printf("Average marks in Mathematics: %.2f\n", f);
    
    return 0;
}

Sample Output:

Input Mathematics marks (0 to terminate): 10                           
15                                                                     
20                                                                     
25                                                                     
0                                                                      
Average marks in Mathematics: 17.50

Flowchart:

C Programming Flowchart: Calculate the average marks of mathematics of some students

C programming Code Editor:

Previous: Write a C program that reads two integers p and q, print p number of lines in a sequence of 1 to b in a line.
Next: Write a C program to calculate the value of S where S = 1 + 1/2 + 1/3 + … + 1/50.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.