In this article, we will write a C program to find the average marks of a student in 5 different subjects. The program takes the marks of 5 subjects as a number(integer) from the user as input, calculates the sum and average of marks of all subjects, and displays the output to the user.
Sample Example:
Input: Enter marks of 5 different subjects: 83 65 39 44 91
Output: Average marks is: 64.400002
Input: Enter marks of 5 different subjects: 67 83 59 76 73
Output: Average marks is: 71.599998
To find the average marks of five subjects, first, we have to find the sum of the marks of all the subjects.
After finding the sum of marks of all the subjects, we will divide the sum by number of subjects. In our case the number of subjects is 5, therefore, we will divide the sum by 5.
After dividing we will get the average marks obtained by the student, and then we will print the output to the output screen using the printf()
function.
Following is the code to find the average marks of 5 different subjects in C programming.
//C Program to find the average of 5 subjects. #include<stdio.h> int main() { //Declare the variables int sub1, sub2, sub3, sub4, sub5; float sum = 0; float average; //Asking user to enter the marks of 5 subjects printf("Enter marks of 5 different subjects:"); scanf("%d %d %d %d %d", &sub1, &sub2, &sub3, &sub4, &sub5); //Calculate the sum of all subjects and store into the variable sum sum = sub1 + sub2 + sub3 + sub4 + sub5; //Calculate the average marks and storing it into the variable average average = sum/5; //Show the output to the user printf("Average marks is: %f", average); return 0; }
Output1:
Enter marks of 5 different subjects:83 65 39 44 91 Average marks is: 64.400002
Output2:
Enter marks of 5 different subjects:67 83 59 76 73 Average marks is: 71.599998
Program Explanation:
To calculate the average marks of 5 different subjects, first, we declared the five variables of type integer sub1, sub2, sub3, sub4, sub5
and two float variables the sum
and average
.
The program then asks the user to enter the marks of 5 subjects which are stored in the sub1, sub2, sub3, sub4, sub5
variables respectively.
We used printf() to display the message asking the user to enter the marks of all subjects, and scanf()
to get the input and store them in the respective variables.
To find the sum of all the subject’s marks, we added them and stored the result in the variable sum
.
After that, we find the average. To find the average, we divide the variable sum by the total number of subjects which is 5, and store the result in the variable average
.
Finally, the output is displayed to the user with the help of printf()
function.
Thank you for reading this article. We hope all your doubts have been cleared. Happy Coding…!