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:- Defines a structure for storing student details and their marks.
- Calculates the total and percentage marks of the student.
- 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
- Write a C Program to Find the Sum and Average of Three Numbers
- Write a C Program to Find the Sum of Individual Digits of a Positive Integer
- Write a C Program to Generate the First N Terms of the Sequence
- Write a C Program to Generate All Prime Numbers Between 1 and N
- Write a C Program to Check Whether the Given Number Is an Armstrong Number or Not
- Write a C program to evaluate an algebraic expression (ax+b)/(ax-b)
- Write a C program to check whether a given number is a perfect number or Not
- Write a C program to check whether a number is a strong number or not
- Write a C program to find the roots of a quadratic equation
- Write a C program to find the factorial of a given integer using a non-recursive function
- Write a C program to find the factorial of a given integer using a recursive function
- Write a C program to find the GCD of two given integers by using the recursive function
- Write a C program to find the GCD of two given integers using a non-recursive function
- Write a C program to find both the largest and smallest number in a list of integers
- Write a C Program to Sort the Array in Ascending Order
- Write a C Program to find whether the given matrix is symmetric or not
- Write a C program to perform the addition of two matrices
- Write a C Program That Uses Functions to Perform Multiplication Of Two Matrices
- Write a C program to use a function to insert a sub-string into a given main string from a given position
- To delete n Characters from a given position in a given string
- Write a C program using user-defined functions to determine whether the given string is palindrome or not
- Write a C program to count the number of lines, words, and characters in a given text
- Write a C program to find the length of the string using Pointer
- Write a C program to Display array elements using calloc( ) function
- Write a C Program to Calculate the Total and Percentage Marks of a Student Using Structure
- Write a C Program to Display the Contents of a File
- Write a C program to copy the contents of one file to another
Comments
Post a Comment