Introduction
Welcome to this comprehensive guide on how to write a C program to calculate the average marks scored by a student for 3 subjects.
Also Read: C Program to Find the Inverse of 2×2 Matrix
In this article, we will walk you through the step-by-step process of creating a C program that efficiently calculates the average marks based on the input provided by the user.
Requirements
To follow along with this tutorial, you will need a basic understanding of the C programming language.
Also Read: C Program to Copy the Contents of One File into Another File
You should have a C compiler installed on your system, such as GCC (GNU Compiler Collection), which is freely available for most operating systems.
Setting Up the Program
To begin, open your preferred text editor or integrated development environment (IDE) and create a new C file. Y
ou can name the file average_calculator.c
or any name of your choice, as long as it has the .c
extension.
Also Read: Getchar and Putchar Function in C with Example
Taking Input from the User
In this section, we will write the code to take input from the user for the marks obtained in three subjects.
We will prompt the user to enter the marks for each subject individually.
#include <stdio.h>
int main() {
int marks1, marks2, marks3;
printf("Enter the marks for Subject 1: ");
scanf("%d", &marks1);
printf("Enter the marks for Subject 2: ");
scanf("%d", &marks2);
printf("Enter the marks for Subject 3: ");
scanf("%d", &marks3);
return 0;
}
Also Read: Best 5 Programs on Fibonacci Series in C
Calculating the Average
Once we have obtained the input from the user, we can proceed to calculate the average of the three subject marks.
We will use the formula (marks1 + marks2 + marks3) / 3
to calculate the average.
#include <stdio.h>
int main() {
int marks1, marks2, marks3;
float average;
// ...code to take input from the user...
average = (marks1 + marks2 + marks3) / 3.0;
return 0;
}
Also Read: Program To Reverse a String in C using Pointer
Displaying the Result
To provide the user with the calculated average, we will add code to display the result on the screen.
#include <stdio.h>
int main() {
int marks1, marks2, marks3;
float average;
// ...code to take input from the user...
average = (marks1 + marks2 + marks3) / 3.0;
printf("The average marks scored by the student: %.2f\n", average);
return 0;
}
Also Read: C Program to Remove Comments and White Spaces from a File
FAQs
The program calculates the average by taking input from the user for the marks obtained in three subjects.
It then applies the formula (marks1 + marks2 + marks3) / 3
to calculate the average.
Yes, you can modify the program to calculate the average for any number of subjects. Simply update the code to include additional variables for the marks and adjust the formula accordingly.
The program assumes that the user will enter valid integer values for the marks. However, if the user enters non-integer values or characters, the program may produce unexpected results. It is essential to validate the user input to ensure the program works correctly.
Yes, it is possible to modify the program to calculate weighted averages. Instead of using a uniform weight for each subject, you can assign different weights to each subject and adjust the formula accordingly.
While this program is specifically designed to calculate the average marks scored by a student, you can modify it to perform other calculations as well. By changing the formula and input prompts, you can adapt the program to suit your specific needs.
To understand this program, it is helpful to have a basic understanding of the C programming language. Familiarity with concepts such as variables, input/output, and arithmetic operations will be beneficial.
Conclusion
Congratulations! You have successfully learned how to write a C program to calculate the average marks scored by a student for 3 subjects.
Also Read: Find the Runner Up Score | Hackerrank Solution
By following the steps outlined in this article, you can create efficient and reliable programs to calculate averages and adapt them to suit your specific requirements.
Remember to validate user input to ensure the program handles various scenarios gracefully.
Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered
Feel free to explore and modify the program to add additional features or functionality.