#include <stdio.h>
// Define the student class
struct Student {
int rollNumber;
char name[50];
int marks[3];
};
int main() {
// Create an instance of the student class
struct Student student;
// Get input from the user
printf("Enter roll number: ");
scanf("%d", &student.rollNumber);
printf("Enter name: ");
scanf("%s", student.name);
printf("Enter marks in three subjects: ");
for (int i = 0; i < 3; i++) {
scanf("%d", &student.marks[i]);
}
// Calculate the total marks
int totalMarks = 0;
for (int i = 0; i < 3; i++) {
totalMarks += student.marks[i];
}
// Display the information stored about the student
printf("Roll number: %d\n", student.rollNumber);
printf("Name: %s\n", student.name);
printf("Total marks: %d\n", totalMarks);
return 0;
}
Explanation:
- We start by including the necessary header file
stdio.h
which contains the functions for input/output operations. - Next, we define a struct
Student
to represent the student class. It has three members: rollNumber
to store the roll number, name
to store the name (as a character array), and marks
to store the marks in three subjects (as an integer array). - In the
main()
function, we create an instance of the Student
struct called student
. - We use
printf()
and scanf()
functions to get input from the user. First, we prompt the user to enter the roll number, name, and marks in three subjects, and then we use scanf()
to read the input and store it in the corresponding members of the student
struct. - We use a loop to calculate the total marks by iterating over the
marks
array and adding up the values. - Finally, we use
printf()
to display the information stored about the student, including the roll number, name, and total marks. - The program ends by returning 0 from the
main()
function.