In: Computer Science
Create a C structure which stores information about a student. Each student should be represented by a student ID (integer), first name, last name, day, month and year of birth, and program code (string). Write a C program which creates an array of 100 of these structures, then prompts the user to enter data from the keyboard to fill the array. If an ID of 0 is entered, data entry should finish, and the list of students should be printed to the screen.
CODE -
#include<stdio.h>
// Create a struct for student
struct student
{
int student_id;
char first_name[20];
char last_name[20];
int day_birth;
int month_birth;
int year_birth;
char program_code[20];
};
// Main Function
int main()
{
// Create an array of 100 structs of student
struct student students[100];
int i=0;
// Read input from the user for different students till user wants to quit by entering 0 as student id
while(i<100)
{
printf("\nEnter the ID of student %d: ", i+1);
scanf("%d", &students[i].student_id);
// Break out of loop if 0 is entered as student id
if(students[i].student_id == 0)
break;
printf("Enter the first name of student %d: ", i+1);
scanf("%s", &students[i].first_name);
printf("Enter the last name of student %d: ", i+1);
scanf("%s", &students[i].last_name);
printf("Enter the day of birth of student %d: ", i+1);
scanf("%d", &students[i].day_birth);
printf("Enter the month of birth of student %d: ", i+1);
scanf("%d", &students[i].month_birth);
printf("Enter the year of birth of student %d: ", i+1);
scanf("%d", &students[i].year_birth);
printf("Enter the program code of student %d: ", i+1);
scanf("%s", &students[i].program_code);
i++;
}
// Display student details
printf("\nStudent Details:\n\n");
for(int j=0; j<i; j++)
{
printf("\nStudent %d\n\n", j+1);
printf("ID: %d\n", students[j].student_id);
printf("First Name: %s\n", students[j].first_name);
printf("Last Name: %s\n", students[j].last_name);
printf("Date of Birth(dd-mm-yyyy): %d-%d-%d\n", students[j].day_birth, students[j].month_birth, students[j].year_birth);
printf("Program Code: %s\n", students[j].program_code);
}
return 0;
}
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.