luc078.c (1942B)
1 /* Create a structure 'student' (Roll, Name, Dept, Course, Year). Write functions to print names by join year and print data by roll number. 2 */ 3 /* Let Us C, Chap- 17 (Structures), Qn No.: B(a) */ 4 5 /* This file is auto-generated by a bot. */ 6 /* This code is not compiled; it is for reference only. */ 7 8 9 #include <stdio.h> 10 #include <string.h> 11 #include <stdlib.h> 12 13 struct student 14 { 15 int roll; 16 char name[50]; 17 char dept[20]; 18 char course[20]; 19 int year; 20 }; 21 22 void print_by_year(struct student *s, int n, int year); 23 void print_by_roll(struct student *s, int n, int roll); 24 25 int main() 26 { 27 struct student data[450] = { 28 {101, "Amit", "CS", "B.Sc", 2024}, 29 {102, "Rahul", "Physics", "B.Sc", 2024}, 30 {103, "Sneha", "CS", "M.Sc", 2023}, 31 {104, "Priya", "Maths", "B.Sc", 2025}, 32 {105, "Rohan", "CS", "B.Sc", 2024} 33 }; 34 int n = 5; // Using 5 sample records 35 int year, roll; 36 37 printf("Enter year to list students: "); 38 scanf("%d", &year); 39 print_by_year(data, n, year); 40 41 printf("\nEnter roll number to find student: "); 42 scanf("%d", &roll); 43 print_by_roll(data, n, roll); 44 45 return 0; 46 } 47 48 void print_by_year(struct student *s, int n, int year) 49 { 50 int i, found = 0; 51 printf("Students joining in %d:\n", year); 52 for (i = 0; i < n; i++) 53 { 54 if (s[i].year == year) 55 { 56 printf("- %s\n", s[i].name); 57 found = 1; 58 } 59 } 60 if (!found) printf("No students found for this year.\n"); 61 } 62 63 void print_by_roll(struct student *s, int n, int roll) 64 { 65 int i; 66 for (i = 0; i < n; i++) 67 { 68 if (s[i].roll == roll) 69 { 70 printf("\n--- Student Details ---\n"); 71 printf("Roll: %d\nName: %s\nDept: %s\nCourse: %s\nYear: %d\n", 72 s[i].roll, s[i].name, s[i].dept, s[i].course, s[i].year); 73 return; 74 } 75 } 76 printf("Student with Roll %d not found.\n", roll); 77 }