Write a C Program to Calculate Total and Percentage Marks of a Student Using Structure

In this blog post, we will explore how to write a simple C program that calculates the total and percentage marks of a student using structures. This example is perfect for beginners who are getting to grips with the basics of structures in C programming.

Understanding the Task

The task is to create a program that:
  1. Defines a structure for storing student details and their marks.
  2. Calculates the total and percentage marks of the student.
  3. Displays the calculated values.

Step 1: Defining the Structure

First, we define a structure named Student. This structure will hold the student's name, roll number, and marks for three subjects.
#include<stdio.h>

struct Student {
    char name[50];
    int rollNo;
    int marks[3];
};

Step 2: Getting Student Data 

We need to input the student’s details and their marks. For simplicity, we will use a single student's data.
struct Student inputStudentData() {
    struct Student s;

    printf("Enter student's name: ");
    scanf("%s", s.name);
    printf("Enter roll number: ");
    scanf("%d", &s.rollNo);
    for(int i = 0; i < 3; i++) {
        printf("Enter marks for subject %d: ", i + 1);
        scanf("%d", &s.marks[i]);
    }

    return s;
}

Step 3: Calculating Total and Percentage 

We will create functions to calculate the total and percentage marks of the student.
int calculateTotal(struct Student s) {
    int total = 0;
    for(int i = 0; i < 3; i++) {
        total += s.marks[i];
    }
    return total;
}

float calculatePercentage(int total) {
    return (float)total / 3;
}

Step 4: Main Function 

In the main() function, we bring everything together. 

We will: 
  • Get the student data. 
  • Calculate the total and percentage. 
  • Display the results.
int main() {
    struct Student student = inputStudentData();

    int total = calculateTotal(student);
    float percentage = calculatePercentage(total);

    printf("\nStudent Name: %s\n", student.name);
    printf("Roll Number: %d\n", student.rollNo);
    printf("Total Marks: %d\n", total);
    printf("Percentage: %.2f%%\n", percentage);

    return 0;
}

Output:

Let’s assume the student's marks are 85, 90, and 95. The output of the program will be:
Enter student's name: John
Enter roll number: 101
Enter marks for subject 1: 85
Enter marks for subject 2: 90
Enter marks for subject 3: 95
Student Name: John
Roll Number: 101
Total Marks: 270
Percentage: 90.00%

The Complete Program with Output

#include<stdio.h>

// Defining the Student structure
struct Student {
    char name[50];
    int rollNo;
    int marks[3];
};

// Function to input student data
struct Student inputStudentData() {
    struct Student s;

    printf("Enter student's name: ");
    scanf("%s", s.name);
    printf("Enter roll number: ");
    scanf("%d", &s.rollNo);
    for(int i = 0; i < 3; i++) {
        printf("Enter marks for subject %d: ", i + 1);
        scanf("%d", &s.marks[i]);
    }

    return s;
}

// Function to calculate total marks
int calculateTotal(struct Student s) {
    int total = 0;
    for(int i = 0; i < 3; i++) {
        total += s.marks[i];
    }
    return total;
}

// Function to calculate percentage
float calculatePercentage(int total) {
    return (float)total / 3;
}

// Main function
int main() {
    struct Student student = inputStudentData();

    int total = calculateTotal(student);
    float percentage = calculatePercentage(total);

    printf("\nStudent Name: %s\n", student.name);
    printf("Roll Number: %d\n", student.rollNo);
    printf("Total Marks: %d\n", total);
    printf("Percentage: %.2f%%\n", percentage);

    return 0;
}

Output:

Enter student's name: John
Enter roll number: 101
Enter marks for subject 1: 85
Enter marks for subject 2: 90
Enter marks for subject 3: 95
Student Name: John
Roll Number: 101
Total Marks: 270
Percentage: 90.00%

Related C Programs with Output

  1. Write a C Program to Find the Sum and Average of Three Numbers
  2. Write a C Program to Find the Sum of Individual Digits of a Positive Integer
  3. Write a C Program to Generate the First N Terms of the Sequence
  4. Write a C Program to Generate All Prime Numbers Between 1 and N
  5. Write a C Program to Check Whether the Given Number Is an Armstrong Number or Not
  6. Write a C program to evaluate an algebraic expression (ax+b)/(ax-b)
  7. Write a C program to check whether a given number is a perfect number or Not
  8. Write a C program to check whether a number is a strong number or not
  9. Write a C program to find the roots of a quadratic equation
  10. Write a C program to find the factorial of a given integer using a non-recursive function
  11. Write a C program to find the factorial of a given integer using a recursive function
  12. Write a C program to find the GCD of two given integers by using the recursive function
  13. Write a C program to find the GCD of two given integers using a non-recursive function
  14. Write a C program to find both the largest and smallest number in a list of integers
  15. Write a C Program to Sort the Array in Ascending Order
  16. Write a C Program to find whether the given matrix is symmetric or not
  17. Write a C program to perform the addition of two matrices
  18. Write a C Program That Uses Functions to Perform Multiplication Of Two Matrices
  19. Write a C program to use a function to insert a sub-string into a given main string from a given position
  20. To delete n Characters from a given position in a given string
  21. Write a C program using user-defined functions to determine whether the given string is palindrome or not
  22. Write a C program to count the number of lines, words, and characters in a given text
  23. Write a C program to find the length of the string using Pointer
  24. Write a C program to Display array elements using calloc( ) function
  25. Write a C Program to Calculate the Total and Percentage Marks of a Student Using Structure
  26. Write a C Program to Display the Contents of a File
  27. Write a C program to copy the contents of one file to another

Comments