Created
March 21, 2023 17:10
-
-
Save newtoallofthis123/d43bc6c49988de07e741756b27b2ec57 to your computer and use it in GitHub Desktop.
C program for Struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
struct Student { | |
char name[100]; | |
int marks; | |
int roll; | |
float grade; | |
}; | |
int main() { | |
int n; | |
printf("Enter the number of students: "); | |
scanf("%d", &n); | |
int option; | |
struct Student students[n]; | |
printf("First, enter the details of students\n"); | |
for(int i = 0; i < n; i++) { | |
printf("Enter the name of student %d: ", i + 1); | |
scanf("%s", students[i].name); | |
printf("Enter the marks of student %d: ", i + 1); | |
scanf("%d", &students[i].marks); | |
printf("Enter the roll number of student %d: ", i + 1); | |
scanf("%d", &students[i].roll); | |
printf("Enter the grade of student %d: ", i + 1); | |
scanf("%f", &students[i].grade); | |
printf("Recorded the details of student %d", i + 1); | |
printf("\n------------------------------------------\n"); | |
} | |
printf("Enter 1 to compare marks\n2 to display the details of students: "); | |
scanf("%d", &option); | |
if(option == 1) { | |
int max = 0; | |
for(int i = 0; i < n; i++) { | |
if(students[i].marks > students[max].marks) { | |
max = i; | |
} | |
} | |
printf("The student with the highest marks is %s with %d marks\n", students[max].name, students[max].marks); | |
} | |
else if(option == 2) { | |
for(int i = 0; i < n; i++) { | |
printf("Name: %s\n", students[i].name); | |
printf("Marks: %d\n", students[i].marks); | |
printf("Roll number: %d\n", students[i].roll); | |
printf("Grade: %f\n", students[i].grade); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment