C Exercises: Read five subject marks (0-100) of a student and calculate the average
C Basic Declarations and Expressions: Exercise-118 with Solution
Calculate the average of marks from 5 subjects
Write a C program that reads five subject marks (0-100) of a student and calculates the average of these marks.
Sample Solution:
C Code:
#include <stdio.h>
int main () {
float marks, tot_marks = 0;
int i = 0, subject = 0;
// Prompt user for input
printf("Input five subject marks(0-100):\n");
// Loop to read marks for five subjects
while (subject != 5) {
// Read a float value 'marks' from user
scanf("%f", &marks);
// Check if 'marks' is within valid range (0-100)
if (marks < 0 || marks > 100) {
printf("Not a valid marks\n"); // Print an error message
}
else {
tot_marks += marks; // Accumulate valid marks
subject++; // Increment subject count
}
}
// Calculate and print the average marks
printf("Average marks = %.2f\n", tot_marks/5);
return 0; // End of program
}
Sample Output:
Input five subject marks(0-100): 75 84 56 98 68 Average marks = 76.20
Flowchart:
C programming Code Editor:
Previous: Write a C program that read two integers and dividing the first number by second, print the result of this division with two digits after the decimal point and print "Division not possible..!" if the division is not possible.
Next: Write a C program to calculate the sum all numbers between two given numbers (inclusive) not divisible by 7.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-118.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics