C Program to Enter Student Marks and Calculate Total Marks per Student

How to Write a C Program to Enter Roll Numbers and Marks for Three Subjects for Three Students and Calculate Their Total Marks

Developing a C program to enter roll numbers and marks for three subjects of three students and calculate their total marks is a simple yet practical task. This step-by-step guide will help you understand the process and provide a code example.

Understanding the Problem

The goal is to write a program using the C language that allows the user to input the roll numbers and marks for three subjects of three students. After collecting this data, the program should calculate the total marks for each student and display the results.

Overview of the C Code

The following C program demonstrates how to achieve this functionality:

include stdio.h
#define NUM_STUDENTS 3
#define NUM_SUBJECTS 3
int main() {
    int rollNo[NUM_STUDENTS];
    int marks[NUM_STUDENTS][NUM_SUBJECTS];
    int total[NUM_STUDENTS];
    // Input roll numbers and marks
    for (int i  0; i  NUM_STUDENTS; i  ) {
        printf(Enter roll number for student %d: , i   1);
        scanf(%d, rollNo[i]);
        printf(Enter marks for subject 1: );
        scanf(%d, marks[i][0]);
        printf(Enter marks for subject 2: );
        scanf(%d, marks[i][1]);
        printf(Enter marks for subject 3: );
        scanf(%d, marks[i][2]);
        total[i]  0; // Initialize total for current student
        for (int j  0; j  NUM_SUBJECTS; j  ) {
            total[i]   marks[i][j]; // Calculate total marks
        }
    }
    // Display results
    printf(Roll No.tSubject 1tSubject 2tSubject 3tTotal Marks
);
    for (int i  0; i  NUM_STUDENTS; i  ) {
        printf(%dtt%dtt%dtt%dtt%d
, rollNo[i], marks[i][0], marks[i][1], marks[i][2], total[i]);
    }
    return 0;
}

Explanation of the Code

Includes and Definitions

The program includes the standard input/output library and defines constants for the number of students and subjects.

Arrays

- rollNo[]: Stores the roll numbers of the students.

- marks[][]: A 2D array that stores the marks for each subject for each student.

- total[]: Stores the total marks obtained by each student.

Input Loop

A loop iterates over each student prompting the user to enter the roll number and marks for three subjects. The total marks for each student are calculated within this loop.

Output Loop

After collecting all the data, another loop prints the roll number and total marks for each student.

Compile and Run the Program

To compile and run the code:

Save the code in a file named student_marks.c. Open a terminal and navigate to the directory where the file is saved. Compile the program using: gcc student_marks.c -o student_marks Run the program using: ./student_marks

This program will prompt you for input and display the total marks for each student afterward.

Additional Tips and Considerations

Here are a few additional tips to make your C program more robust:

Include error checking for input to handle invalid data. Use functions to separate the input, calculation, and output logic for better code reusability and readability. Consider using structures to store student details, which can simplify the code and make it more modular.