Assignment For C Struct
Assignment For C Struct
XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU, NEPAL
Phone: 01-5321365, 01-5344636
Email: [email protected]
“C PROGRAMMING WORKING
WITH FUNCTIONS”
Submitted By Submitted To Signature
Name: Mr. Mrigendra Pradhan
Roll No: Department of Computer
Class: Science,
Section: St. Xavier’s College
Submission Date: 27th August, 2023
Table of Contents
1. Write a C program that inputs roll, name and age of a student using Structure and display it..........3
2. Write a C program that takes vehicle number, model and color of a car using Structure and print it
using Function.......................................................................................................................................4
3. Write a C program that inputs the id, name, and price of N number of books using Structure and
print them.............................................................................................................................................5
4. Write a C program that inputs the name and address of 5 students using Structure and arranges
them alphabetically by name of student...............................................................................................6
5. Write a C program to enter the name and marks of 3 subjects of N number of students and
calculate the percentage of each student using Structure....................................................................6
6. Write a C program that takes the name and age of 5 employees and sort the data in ascending
order by age using Structure.................................................................................................................8
7. Write a C program to define a structure called Staff with members: id, name, post, and salary. Sort
the records of staff in order of maximum salary..................................................................................10
8. Write a C program to take data of 2 products including id, name and price using Structure and
print the expensive product using Function........................................................................................12
9. Write a C program that takes vehicle number, model and color of a car using Union and print it...13
10. Write a C program to demonstrate storage size difference between structure and union. Create
structure and union with same members............................................................................................13
1. Write a C program that inputs roll, name and age of a student using Structure and
display it.
Code: #include <stdio.h>
struct student {
int roll;
char name[50];
int age;
};
int main() {
struct student s;
scanf("%d", &s.roll);
scanf("%s", &s.name);
scanf("%d", &s.age);
return 0;
Output:
2. Write a C program that takes vehicle number, model and color of a car using
Structure and print it using Function.
Code: #include <stdio.h>
struct car {
char vehicle_number[10];
char model[50];
char color[20];
};
int main() {
scanf("%s", car.vehicle_number);
scanf("%s", car.model);
scanf("%s", car.color);
print_car(car);
return 0; }
Output:
3. Write a C program that inputs the id, name, and price of N number of books using
Structure and print them.
Code:
#include <stdio.h>
struct book {
int id;
char name[50];
int price;
};
int main() {
int i,n;
scanf("%d", &n);
scanf("%d", &b[i].id);
scanf("%s", b[i].name);
scanf("%d", &b[i].price);
return 0;
}
Output:
4. Write a C program that inputs the name and address of 5 students using Structure
and arranges them alphabetically by name of student.
#include <stdio.h>
#include <string.h>
struct student {
char name[50];
char address[100];
};
int main() {
int i, j;
scanf("%s", s[i].name);
scanf("%s", s[i].address);
s[i] = s[j];
s[j] = temp;
}
for (i = 0; i < 5; i++) {
return 0;
Output:
struct student {
char name[50];
int m1;
int m2;
int m3;
int percentage;
};
int main() {
int n,i;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", s[i].name);
scanf("%d", &s[i].m1);
scanf("%d", &s[i].m2);
scanf("%d", &s[i].m3);
return 0;
Output:
6. Write a C program that takes the name and age of 5 employees and sort the data in
ascending order by age using Structure.
Code: #include <stdio.h>
#include <string.h>
struct employee {
char name[50];
int age;
};
int main() {
int i,j;
scanf("%s", e[i].name);
scanf("%d", &e[i].age);
e[i] = e[j];
e[j] = temp;
}} }
return 0;
}
Output:
7. Write a C program to define a structure called Staff with members: id, name, post,
and salary. Sort the records of staff in order of maximum salary.
Code: #include <stdio.h>
#include <string.h>
struct staff {
int id;
char name[50];
char post[50];
int salary;
};
int main() {
int i,j;
scanf("%d", &s[i].id);
scanf("%s", s[i].post);
scanf("%d", &s[i].salary);
temp = s[i];
s[i] = s[j];
s[j] = temp;
}} }
return 0;
Ouput:
8. Write a C program to take data of 2 products including id, name and price using
Structure and print the expensive product using Function.
struct prod {
int id;
char name[20];
int price;
{ int i,j;
temp = x[i];
x[i] = x[j];
x[j] = temp;
} } }}
int main() {
int i;
scanf("%d", &p[i].id)
scanf("%s", p[i].name);
scanf("%d", &p[i].price);
sort(p, 2);
return 0;
Output:
9. Write a C program that takes vehicle number, model and color of a car using Union
and print it.
Code: #include <stdio.h>
union details {
char vehicle_number[10];
char model[20];
char color[10];
int main() {
union details c;
scanf("%s", c.vehicle_number);
scanf("%s", c.model);
scanf("%s", c.color)
printf("Vehicle number: %s\n", c.vehicle_number);
return 0;
Output:
10. Write a C program to demonstrate storage size difference between structure and
union. Create structure and union with same members.
Code: #include <stdio.h>
struct detais {
char vehicle_number[10];
char model[20];
char color[10];
};
union details {
char vehicle_number[10];
char model[20];
char color[10];
};
int main() {
return 0;
Output: