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

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];
}
}

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];
}
}

// 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%
```