Tech Study

Write a C program to calculate percentage of student using structure

Introduction to C Language

C is a general-purpose, high-level programming language that is widely used for developing software applications, operating systems, and embedded systems. It is a powerful language that is easy to learn, and it is widely used for both system programming and application development. In this article, we will be discussing how to take percentage of marks using C language and structure.

Introduction to Calculating Percentage of Marks using C Language and Structure

Calculating percentage of marks is a common task that is required in many educational systems. In order to calculate percentage of marks, we need to divide the total marks obtained by the total marks available and then multiply the result by 100. This will give us the percentage of marks obtained by the student. In this article, we will be discussing how to calculate percentage of marks using C language and structure.

Code

The following code shows how to calculate percentage of marks using C language and structure.

#include <stdio.h>

struct student

{

    int roll_no;

    char name[20];

    float marks[3];

    float percent;

};

void main()

{

    int i;

    struct student s[3];

    for (i = 0; i < 3; i++)

    {

        printf("Enter the roll number of student %d: ", i + 1);

        scanf("%d", &s[i].roll_no);

        printf("Enter the name of student %d: ", i + 1);

        scanf("%s", s[i].name);

        printf("Enter the marks of student %d in three subjects: ", i + 1);

        scanf("%f%f%f", &s[i].marks[0], &s[i].marks[1], &s[i].marks[2]);

        s[i].percent = (s[i].marks[0] + s[i].marks[1] + s[i].marks[2]) / 3;

    }

    printf("\nRoll Number\tName\tMarks\tPercentage\n");

    for (i = 0; i < 3; i++)

    {

        printf("%d\t\t%s\t%.2f\t%.2f\n", s[i].roll_no, s[i].name, s[i].marks[0] + s[i].marks[1] + s[i].marks[2], s[i].percent);

    }

}

Explanation

In the above code, we have created a structure called “student” which contains four variables: roll_no, name, marks, and percent. The roll_no and name variables are used to store the roll number and name of the student respectively, while the marks variable is an array that stores the marks of the student in three subjects. The percent variable is used to store the percentage of marks obtained by the student.

We then use a for loop to take input from the user for the roll number, name, and marks of three students. We then calculate the percentage of marks obtained by each student by dividing the total marks obtained by the total marks available and then multiplying the result by 100. This is done by using the following formula:

percent = (marks[0] + marks[1] + marks[2]) / 3;

We then use another for loop to print the roll number, name, total marks, and percentage of marks for each

student. The output will be displayed in a tabular format, making it easy to read and understand.

Result

When the above code is executed, the user will be prompted to enter the roll number, name, and marks of three students. The program will then calculate the percentage of marks obtained by each student and display the results in a tabular format. The output will look something like this:

Roll Number Name Marks Percentage 1 John 300 100 2 Jane 285 95 3 Michael 277 92.3

Conclusion

In this article, we have discussed how to calculate the percentage of marks using C language and structure. We have seen how to use a structure to store the details of multiple students and how to use for loops to take input and display output. We have also seen how to use a simple mathematical formula to calculate the percentage of marks obtained by a student. This is a basic example of how to use C language and structure to perform a specific task, and it can be extended to more complex applications as needed. Overall, C language is a powerful tool that can be used to create a variety of software applications, and understanding how to use structures is an important step in mastering this language.

 

New to coding world or want to learn more about coding , Here are some of the book we highly recommend you should read once in your lifetime.
6 Best Coding Books for Beginners You Must Read in 2023

TaggedWrite a C program to calculate percentage of student using structure

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more