Structure and Union Solution
1. Write a
program to read records of 3 employees and then display information using
structure array.
#include <stdio.h>
struct Employee {
char name[20];
int age;
float salary;
};
int main() {
struct Employee emp[3];
int i;
for(i = 0; i < 3;
i++) {
printf("Enter
name, age, and salary of employee %d: ", i + 1);
scanf("%s %d
%f", emp[i].name, &emp[i].age, &emp[i].salary);
}
printf("Employee
Records:\n");
for(i = 0; i < 3;
i++) {
printf("Name:
%s\n", emp[i].name);
printf("Age:
%d\n", emp[i].age);
printf("Salary:
%.2f\n", emp[i].salary);
printf("\n");
}
return 0;
}
Output
Enter details for
employee 1: Name: A Age: 28 Salary: 2500
Enter details for
employee 2: Name: B Age: 25 Salary: 2000
Enter details for
employee 3: Name: C Age: 30 Salary: 15000
Employee Records:
Name: A Age: 28 Salary:
2500.00
Name: B Age: 25 Salary: 20000.00
Name: C Age: 30 Salary: 15000.00
3. Write a program to read name, roll and
marks in 3 subjects and display average marks of subjects using structure.
#include <stdio.h>
struct Student {
char name[30];
int roll;
float marks[3];
};
int main() {
struct Student stu;
float totalMarks = 0,
avgMarks;
int i;
printf("Enter
student name: ");
scanf("%[^\n]", stu.name);
printf("Enter roll
number: ");
scanf("%d",
&stu.roll);
for (i = 0; i < 3;
i++) {
printf("Enter
marks in subject %d: ", i + 1);
scanf("%f", &stu.marks[i]);
totalMarks +=
stu.marks[i];
}
avgMarks = totalMarks /
3;
printf("\nStudent
Record:\n");
printf("Name:
%s\n", stu.name);
printf("Roll:
%d\n", stu.roll);
printf("Marks in
Subject 1: %.2f\n", stu.marks[0]);
printf("Marks in
Subject 2: %.2f\n", stu.marks[1]);
printf("Marks in
Subject 3: %.2f\n", stu.marks[2]);
printf("Average
marks: %.2f\n", avgMarks);
return 0;
}
Enter student name: Program
Enter roll number: 1
Enter marks in subject 1: 85
Enter marks in subject 2: 90
Enter marks in subject 3: 75
Student Record:
Name: Program
Roll: 1
Marks in Subject 1: 85.00
Marks in Subject 2: 90.00
Marks in Subject 3: 75.00
Average marks: 83.33
7. Write a
program to store details of an employee using nested structure.
#include
<stdio.h>
struct Date {
int day;
int month;
int year;
};
struct
Employee {
char name[30];
int id;
float salary;
struct Date date_of_joining;
};
int main() {
struct Employee employee;
printf("Enter name of employee:
");
scanf("%[^\n]", employee.name);
printf("Enter ID of employee: ");
scanf("%d", &employee.id);
printf("Enter salary of employee:
");
scanf("%f",
&employee.salary);
printf("Enter date of joining
(DD/MM/YYYY) of employee: ");
scanf("%d/%d/%d",&employee.date_of_joining.day,&employee.date_of_joining.month,&employee.date_of_joining.year);
printf("\nDetails of
employee:\n");
printf("Name: %s\n",
employee.name);
printf("ID: %d\n", employee.id);
printf("Salary: %.2f\n",
employee.salary);
printf("Date of joining:
%02d/%02d/%04d\n", employee.date_of_joining.day, employee.date_of_joining.month,
employee.date_of_joining.year);
return 0;
}
|
Output Enter
name of employee: Sandeep Enter ID
of employee: 1001 Enter
salary of employee: 25000 Enter
date of joining (DD/MM/YYYY) of employee: 01/01/2023 Details
of employee: Name:
Sandeep ID: 1001 Salary:
25000.00 Date of
joining: 01/01/2023 |
5. Write a program to reads which reads different roll no name
marks of n students and arrange them into alphabetical order of name (sort them
in ascending according to name ) using structure.
#include
<stdio.h>
struct
student {
int roll_no;
char name[50];
int marks;
};
int
compare_names(const void* a, const void* b) {
struct student* s1 = (struct student*)a;
struct student* s2 = (struct student*)b;
return strcmp(s1->name, s2->name);
}
int main() {
int n, i;
printf("Enter the number of students:
");
scanf("%d", &n);
struct student students[n];
for (i = 0; i < n; i++) {
printf("Enter details for student
%d:\n", i+1);
printf("Roll No: ");
scanf("%d", &students[i].roll_no);
printf("Name: ");
scanf("%s",
students[i].name);
printf("Marks: ");
scanf("%d",
&students[i].marks);
}
qsort(students, n, sizeof(struct student),
compare_names);
printf("\nSorted List of Students:\n");
for (i = 0; i < n; i++) {
printf("Roll No: %d, Name: %s,
Marks: %d\n", students[i].roll_no, students[i].name, students[i].marks);
}
return 0;
}
Enter the
number of students: 2
Enter
details for student 1:
Roll No:
001
Name: ABC
Marks: 75
Enter
details for student 2:
Roll No:
002
Name: DEF
Marks: 80
Sorted
List of Students:
Roll No:
1, Name: ABC, Marks: 75
Roll No:
2, Name: DEF, Marks: 80
To run program please click me.
Comments
Post a Comment