Advertisement

Students' Record Nested Structure C Program




#include <stdio.h>
#include <string.h>
#define SIZE 3
typedef struct Date {
    int day;
    int month;
    int year;
} dt;
typedef struct StudentName {
    char fname[50];
    char mname[50];
    char lname[50];
} stnam;
typedef struct StudentDetails {
    int roll;
    stnam nam;
    int age;
    char gender[10];
    dt dob;
    int marks[5];
    char grade[2];
} stud;
int main() {
    stud s[SIZE];
    int i = 0, total, j;
    float average;
    while (i < SIZE) {
        total = 0;  // Reset total for each student
        average = 0.0;
        puts("------------------------------------");
        printf("\n Enter details of student number:%d\n", i + 1);
        
        puts("Enter Roll Number:");
        scanf("%d", &s[i].roll);
        puts("Enter Age:");
        scanf("%d", &s[i].age);
        puts("Enter Date of Birth (day month year):");
        scanf("%d %d %d", &s[i].dob.day, &s[i].dob.month, &s[i].dob.year);
        for (j = 0; j < 5; j++) {
            printf("Enter marks of subject %d:\n", j + 1);
            scanf("%d", &s[i].marks[j]);
            total += s[i].marks[j];
        }
        average = total / 5.0;
        if (average > 90 && average <= 100) {
        strcpy(s[i].grade, "O");
        } else if (average > 80 && average <= 89) {
        strcpy(s[i].grade, "E");
        } else if (average > 70 && average <= 79) {
        strcpy(s[i].grade, "A");
        } else if (average > 60 && average <= 69) {
        strcpy(s[i].grade, "B");
        } else if (average > 50 && average <= 59) {
        strcpy(s[i].grade, "C");
        } else if (average > 40 && average <= 49) {
        strcpy(s[i].grade, "D");
        } else {
        strcpy(s[i].grade, "F");
    }
        
        puts("Enter first name:");
        scanf("%s", s[i].nam.fname);
        puts("Enter middle name:");
        scanf("%s", s[i].nam.mname);
        puts("Enter last name:");
        scanf("%s", s[i].nam.lname);
        puts("Enter Gender:");
        scanf("%s", s[i].gender);
        puts("\n------------------------------------");
        printf("\nThe entered details of student number:%d are\n", i + 1);
        printf("Roll: %d\n", s[i].roll);
        printf("Age: %d\n", s[i].age);
        printf("Date of Birth: %d/%d/%d\n", s[i].dob.day, s[i].dob.month, s[i].dob.year);
        for (j = 0; j < 5; j++) {
            printf("Entered marks of subject %d: %d\n", j + 1, s[i].marks[j]);
        }
        
        printf("Total Marks: %d\n", total);
        printf("Grade: %s\n", s[i].grade);
        printf("Name: %s %s %s\n", s[i].nam.fname, s[i].nam.mname, s[i].nam.lname);
        printf("Gender: %s\n", s[i].gender);
        puts("\n------------------------------------\n");
        i++;
    }
    return 0;
}


Explanation:-

  • #include <stdio.h> includes the standard input-output library for functions like printf and scanf.
  • #include <string.h> includes the string library for functions like strcpy.
  • #define SIZE 3 defines a constant SIZE that specifies the number of students. In this case, it is set to 3.
  • Date structure holds the day, month, and year.
  • StudentName structure holds the first name, middle name, and last name of a student.
  • StudentDetails structure holds all details about a student including roll number, name (as a StudentName structure), age, gender, date of birth (as a Date structure), marks for 5 subjects, and grade.
  • Main Loop Execution

    • Initialization:

      • An array s of stud structures is declared to hold details of SIZE (3) students.
      • i is used to iterate over the students.
      • total and average are used to calculate and store the total marks and average marks of a student respectively.
    • Input Loop:

      • The while loop runs for each student from 0 to SIZE - 1.
      • total and average are reset for each student to ensure accurate calculations.
      • Various student details are input using scanf.
    • Grade Calculation:

      • Marks for 5 subjects are input and accumulated into total.
      • average is calculated by dividing total by 5.
      • Based on the average, a grade is assigned to the student by copying a grade character to s[i].grade.
    • String Inputs:

      • Student's first name, middle name, last name, and gender are input using scanf.
    • Output:


  • Post a Comment

    0 Comments