CP Exp 18
CP Exp 18
AIM: -
Demonstrate structure and union to specify data on students like Roll-number, Name,
Department, Course, Year of Joining and Print the data of student according to roll
number
Algorithm:
1. Start.
2. Define a Union CourseInfo:
o Define a character array courseName[50] for the course name.
o Define an integer courseCode (optional, if needed for different
representation).
3. Define a Structure Student:
o Declare an integer variable rollNumber.
o Declare a character array name[50].
o Declare a character array department[50].
o Declare a variable of type CourseInfo (union).
o Declare an integer variable yearOfJoining.
4. Initialize Variables:
o Declare an array of Student structures to hold multiple records (e.g.,
students[100]).
o Declare an integer variable studentCount initialized to 0.
o Declare an integer variable rollNumberToFind.
5. Input Loop:
o Repeat the following steps until the user decides to stop:
1. Prompt the user to enter the roll number.
2. Read and store the roll number in the current
students[studentCount].rollNumber.
3. Prompt the user to enter the name.
4. Read and store the name in students[studentCount].name.
5. Prompt the user to enter the department.
6. Read and store the department in
students[studentCount].department.
7. Prompt the user to enter the course name.
8. Read and store the course name in
students[studentCount].course.courseName.
9. Prompt the user to enter the year of joining.
10. Read and store the year of joining in
students[studentCount].yearOfJoining.
11. Increment studentCount by 1.
12. Ask the user if they want to add another student (if 'y',
continue; otherwise, exit the loop).
6. Search for Student:
o Prompt the user to enter a roll number to search.
o Read and store the roll number in rollNumberToFind.
7. Search Loop:
o Initialize a variable found to 0 (indicating not found).
o Loop through the students array from 0 to studentCount - 1:
1. If students[i].rollNumber equals rollNumberToFind:
● Call the function to print student details.
● Set found to 1 (indicating found).
● Break the loop.
8. Check If Found:
o If found is still 0 after the loop, print "Student not found".
End.
Program/Code:
Structure
#include <stdio.h>
#include <string.h>
int main() {
struct Student students[5]; // Array to store data for 5 students
int n = 5; // Total number of students
int rollNumberToSearch;
printf("Department: ");
fgets(students[i].department, sizeof(students[i].department), stdin);
students[i].department[strcspn(students[i].department, "\n")] = 0;
printf("Course: ");
fgets(students[i].course, sizeof(students[i].course), stdin);
students[i].course[strcspn(students[i].course, "\n")] = 0;
if (!found) {
printf("No student found with roll number %d.\n", rollNumberToSearch);
}
return 0;
}
UNION
#include <stdio.h>
#include <string.h>
int main() {
// Here, we use a union for one student's data
union StudentUnion student;
// Assign data to the union (but only one field can be used at a time)
student.rollNumber = 101;
strcpy(student.name, "John Doe");
strcpy(student.department, "Computer Science");
strcpy(student.course, "B.Tech");
student.yearOfJoining = 2020;
return 0;
}
Output:
Input
Output