# C Program to Calculate Average Marks Scored by a Student

## 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.

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.

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.

## 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;
}``````

## 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;
}``````

## FAQs

Q: How does the program calculate the average?

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.

Q: Can I calculate the average for more than three subjects?

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.

Q: What if the user enters invalid input?

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.

Q: Is it possible to modify the program to calculate weighted averages?

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.

Q: Can I use this program for other calculations besides average marks?

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.

Q: Are there any prerequisites for understanding this program?

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.

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.

Feel free to explore and modify the program to add additional features or functionality.