Write a C Program that defines a structure to store the following student data CGPA,

Question

Write a C Program that defines a structure to store the following student data CGPA, courses (course name, GPA), address (consisting of a street address, city, state, zip). Input 2 student records, compare and display which student has highest GPA in which course also display which student has highest CGPA.

Summary

In this question, we will construct a structure for a student and his details in C language. In structure, we will store the data like the name, address, and marks of the student. Then we will take input data for 2 students from the user and then we will compare the marks in subjects. We will compare the marks in different subjects and show the output.

Explanation

The structure is a user-defined datatype in the C language. The structure contains a collection of different types of data types as an array contains a collection of the same type of data type. We define the structure using the “struct” keyword. And, we initialize the name of a structure variable with lowercase letters.

We will define a structure with the name of “Student” here. The student structure will have the student details information like name, CGPA, address, course list. We also create another structure to store the course.

In the main() function, we will define two objects of student struct type and we will take the details of them from the user. Then we will compare the GPA of two students and print the required output. For comparing, we will use operators (>,<,==). 

For better understanding, we have attached the code of the program along with its output.

Code

#include<stdio.h>
#include<conio.h>

//structure defination for the course
struct course
{   
    float gpa;
    char course_name[10];
};

//structure defination for the student
struct student
{	
    char name[10];
    float cgpa;
    char add[20];
    struct course c[2];
};

//main function
int main()
{
    //defining two student object std1 and std2
    struct student std1,std2;
    int i,j;
    
    //input data of 2 students
    printf("Enter the details for 1st student: ");
    printf("\nEnter Student Name: ");
    scanf("%s",std1.name);
    fflush(stdout);
    printf("Enter CGPA: ");
    scanf("%f",&std1.cgpa);
    printf("Enter Address :");
    scanf("%s",std1.add);
    fflush(stdout);
    printf("Enter 2 course name and GPA: ");
    printf("\nCourse 1: ");
    scanf("%s",std1.c[0].course_name);
    fflush(stdout);
    scanf("%f",&std1.c[0].gpa);
    printf("Course 2: ");
    scanf("%s",std1.c[1].course_name);
    fflush(stdout);
    scanf("%f",&std1.c[1].gpa);
    
    printf("Enter the details for 2nd student: ");
    printf("\nEnter Student Name: ");
    scanf("%s",std2.name);
    fflush(stdout);
    printf("Enter CGPA: ");
    scanf("%f",&std2.cgpa);
    printf("Enter Address :");
    scanf("%s",std2.add);
    fflush(stdout);
    printf("Enter 2 course name and GPA: ");
    printf("\nCourse 1: ");
    scanf("%s",std2.c[0].course_name);
    fflush(stdout);
    scanf("%f",&std2.c[0].gpa);
    printf("Course 2: ");
    scanf("%s",std2.c[1].course_name);
    fflush(stdout);
    scanf("%f",&std2.c[1].gpa);
    
    //compare and print information
    for(i=0; i<2; i++)
    {	if(std1.c[i].gpa > std2.c[i].gpa)
            printf("\n%s has more GPA %f in %s",std1.name, std1.c[i].gpa, std1.c[i].course_name);
        if(std1.c[i].gpa < std2.c[i].gpa)
            printf("\n%s has more GPA %f in %s",std2.name, std2.c[i].gpa, std2.c[i].course_name);
        if(std1.c[i].gpa == std2.c[i].gpa)
            printf("\nBoth have equal GPA");
    }
    if(std1.cgpa> std2.cgpa)
        printf("\n %s has more CGPA",std1.name);
    else
        printf("\n %s has more CGPA",std2.name);
        
}

 

Output

 

C program that defines a structure

 

 

Also read, Symbolic Constant in C

 

Share this post

Leave a Reply

Your email address will not be published.