0% found this document useful (0 votes)
9 views

Assignment For C Struct

Uploaded by

go away
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assignment For C Struct

Uploaded by

go away
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ST.

XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU, NEPAL
Phone: 01-5321365, 01-5344636
Email: [email protected]

LAB/WRITTEN ASSIGNMENT NUMBER:

“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;

printf("Enter roll number: ");

scanf("%d", &s.roll);

printf("Enter name: ");

scanf("%s", &s.name);

printf("Enter age: ");

scanf("%d", &s.age);

printf("Roll number: %d\n", s.roll);

printf("Name: %s\n", s.name);

printf("Age: %d\n", 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 print_car(struct car c) {

printf("Vehicle number: %s\n", c.vehicle_number);

printf("Model: %s\n", c.model);

printf("Color: %s\n", c.color);

int main() {

struct car car;

printf("Enter vehicle number: ");

scanf("%s", car.vehicle_number);

printf("Enter model: ");

scanf("%s", car.model);

printf("Enter color: ");

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;

struct book b[100];

printf("Enter number of books : ");

scanf("%d", &n);

for (i = 0; i < n; i++) {

printf("Enter book id: ");

scanf("%d", &b[i].id);

printf("Enter book name: ");

scanf("%s", b[i].name);

printf("Enter book price: ");

scanf("%d", &b[i].price);

for (i = 0; i < n; i++) {

printf("Book id: %d\n", b[i].id);

printf("Book name: %s\n", b[i].name);

printf("Book price: %d\n", 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;

struct student s[5];

for (i = 0; i < 5; i++) {

printf("Enter name of student %d: ", i + 1);

scanf("%s", s[i].name);

printf("Enter address of student %d: ", i + 1);

scanf("%s", s[i].address);

for (i = 0; i < 5; i++) {

for (j = i + 1; j < 5; j++) {

if (strcmp(s[i].name, s[j].name) > 0) {

struct student temp = s[i];

s[i] = s[j];

s[j] = temp;

}
for (i = 0; i < 5; i++) {

printf("Name of student : %s\n", s[i].name);

printf("Address of student : %s\n", s[i].address);

return 0;

Output:

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.
Code: #include <stdio.h>

struct student {

char name[50];

int m1;

int m2;

int m3;

int percentage;

};

int main() {

int n,i;

struct student s[100];

printf("Enter number of students: ");

scanf("%d", &n);
for (int i = 0; i < n; i++) {

printf("Enter name of student : ");

scanf("%s", s[i].name);

printf("Enter marks of subject 1 : ");

scanf("%d", &s[i].m1);

printf("Enter marks of subject 2 : ");

scanf("%d", &s[i].m2);

printf("Enter marks of subject 3 :");

scanf("%d", &s[i].m3);

for ( i = 0; i < n; i++) {

s[i].percentage = (s[i].m1 + s[i].m2 + s[i].m3) / 3;

for ( i = 0; i < n; i++) {

printf("Name of student : %s\n", s[i].name);

printf("Percentage of student : %d\n", s[i].percentage);

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() {

struct employee e[5];

int i,j;

for ( i = 0; i < 5; i++) {

printf("Enter name of employee : ");

scanf("%s", e[i].name);

printf("Enter age of employee : ");

scanf("%d", &e[i].age);

for ( i = 0; i < 5; i++) {

for ( j = i + 1; j < 5; j++) {

if (e[i].age > e[j].age) {

struct employee temp = e[i];

e[i] = e[j];

e[j] = temp;

}} }

for ( i = 0; i < 5; i++) {

printf("Name: %s\n", e[i].name);

printf("Age: %d\n", e[i].age);

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() {

struct staff s[5],temp;

int i,j;

for (i = 0; i < 2; i++) {

printf("Enter id of staff : ");

scanf("%d", &s[i].id);

printf("Enter name of staff ");


scanf("%s", s[i].name);

printf("Enter post of staff ");

scanf("%s", s[i].post);

printf("Enter salary of staff ");

scanf("%d", &s[i].salary);

for ( i = 0; i < 2; i++) {

for ( j = i + 1; j < 2; j++) {

if (s[i].salary < s[j].salary) {

temp = s[i];

s[i] = s[j];

s[j] = temp;

}} }

for ( i = 0; i < 2; i++) {

printf("ID: %d\n", s[i].id);

printf("Name: %s\n", s[i].name);

printf("Post: %s\n", s[i].post);

printf("Salary: %d\n", s[i].salary);

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.

Code: #include <stdio.h>

struct prod {

int id;

char name[20];

int price;

int sort(struct prod x[], int n)

{ int i,j;

struct prod temp;

for ( i = 0; i < n; i++) {

for (j = i + 1; j < n; j++) {

if (x[i].price > x[j].price) {

temp = x[i];

x[i] = x[j];

x[j] = temp;

} } }}

int main() {

struct prod p[2];

int i;

for ( i = 0; i < 2; i++) {

printf("Enter product ID: ");

scanf("%d", &p[i].id)

printf("Enter product name: ");

scanf("%s", p[i].name);

printf("Enter product price: ");

scanf("%d", &p[i].price);

sort(p, 2);

printf("The most expensive product is: \n");

printf("ID: %d\n", p[1].id);


printf("Name: %s\n", p[1].name);

printf("Price: %d\n", p[1].price);

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;

printf("Enter vehicle number: ");

scanf("%s", c.vehicle_number);

printf("Enter model: ");

scanf("%s", c.model);

printf("Enter color: ");

scanf("%s", c.color)
printf("Vehicle number: %s\n", c.vehicle_number);

printf("Model: %s\n", c.model);

printf("Color: %s\n", c.color);

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() {

struct detais c1;

union details c2;

printf("The storage size of the structure is %u .\n", sizeof(c1));

printf("The storage size of the union is %u .\n", sizeof(c2));

return 0;

Output:

You might also like