Practical / Solution
Advantages of Structures — Exercise 1
Problem
Write a C program to store student roll number, name and marks
together using a structure and display the complete record.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s;
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter name: ");
scanf("%49s", s.name);
printf("Enter marks: ");
scanf("%f", &s.marks);
// Display related data as one record
printf("\nStudent Record:\n");
printf("Roll = %d\n", s.roll);
printf("Name = %s\n", s.name);
printf("Marks = %.2f\n", s.marks);
return 0;
}
Explanation
One major advantage of a structure is that related data can be grouped
into a single logical record. This makes the program easier to organize.
Expected Output
Enter roll number: 101
Enter name: Rahul
Enter marks: 85.5
Student Record:
Roll = 101
Name = Rahul
Marks = 85.50
Advantages of Structures — Exercise 2
Problem
Write a C program to store the details of an employee using a structure
containing ID, name and salary, and calculate the annual salary.
Program
#include <stdio.h>
struct Employee
{
int id;
char name[50];
float salary;
};
int main()
{
struct Employee emp;
float annualSalary;
printf("Enter employee ID: ");
scanf("%d", &emp.id);
printf("Enter name: ");
scanf("%49s", emp.name);
printf("Enter monthly salary: ");
scanf("%f", &emp.salary);
// Calculate annual salary
annualSalary = emp.salary * 12;
printf("\nEmployee ID = %d\n", emp.id);
printf("Name = %s\n", emp.name);
printf("Annual Salary = %.2f\n", annualSalary);
return 0;
}
Explanation
A structure keeps all employee-related information together.
This improves readability and makes calculations using the record convenient.
Expected Output
Enter employee ID: 501
Enter name: Amit
Enter monthly salary: 40000
Employee ID = 501
Name = Amit
Annual Salary = 480000.00
Advantages of Structures — Exercise 3
Problem
Write a C program to store the details of a book using a structure
and calculate its price after discount.
Program
#include <stdio.h>
struct Book
{
int id;
char title[100];
float price;
};
int main()
{
struct Book book;
float discount, finalPrice;
printf("Enter book ID: ");
scanf("%d", &book.id);
printf("Enter title: ");
scanf("%99s", book.title);
printf("Enter price: ");
scanf("%f", &book.price);
printf("Enter discount percentage: ");
scanf("%f", &discount);
// Calculate discounted price
finalPrice = book.price - (book.price * discount / 100);
printf("\nBook ID = %d\n", book.id);
printf("Title = %s\n", book.title);
printf("Final Price = %.2f\n", finalPrice);
return 0;
}
Explanation
Structures make it convenient to keep different types of information
about the same object together. Here, book identification and pricing
information are stored in one record.
Expected Output
Enter book ID: 101
Enter title: Programming
Enter price: 500
Enter discount percentage: 10
Book ID = 101
Title = Programming
Final Price = 450.00
Advantages of Structures — Exercise 4
Problem
Write a C program to create two student records using a structure
and compare their marks to find the higher scorer.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s1, s2;
printf("Enter first student details: ");
scanf("%d %49s %f",
&s1.roll,
s1.name,
&s1.marks);
printf("Enter second student details: ");
scanf("%d %49s %f",
&s2.roll,
s2.name,
&s2.marks);
// Compare student records using their members
if (s1.marks > s2.marks)
printf("%s scored higher marks.\n", s1.name);
else if (s2.marks > s1.marks)
printf("%s scored higher marks.\n", s2.name);
else
printf("Both students scored equal marks.\n");
return 0;
}
Explanation
Multiple records of the same type can be created easily using
structure variables. Each record keeps its own related data together.
Expected Output
Enter first student details: 101 Rahul 72
Enter second student details: 102 Priya 88
Priya scored higher marks.
Advantages of Structures — Exercise 5
Problem
Write a C program to store the dimensions of a rectangle in a structure
and calculate both area and perimeter using the same record.
Program
#include <stdio.h>
struct Rectangle
{
float length;
float width;
};
int main()
{
struct Rectangle r;
float area, perimeter;
printf("Enter length and width: ");
scanf("%f %f", &r.length, &r.width);
// Use structure members in calculations
area = r.length * r.width;
perimeter = 2 * (r.length + r.width);
printf("Area = %.2f\n", area);
printf("Perimeter = %.2f\n", perimeter);
return 0;
}
Explanation
Structures improve organization by keeping related values together.
The same structure record can then be used for several calculations.
Expected Output
Enter length and width: 10 5
Area = 50.00
Perimeter = 30.00
Advantages of Structures — Exercise 6
Problem
Write a C program to store a student's details and determine whether
the student has passed based on marks stored in the structure.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s;
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter name: ");
scanf("%49s", s.name);
printf("Enter marks: ");
scanf("%f", &s.marks);
// Use the structure member for decision making
if (s.marks >= 50)
printf("%s has passed.\n", s.name);
else
printf("%s has failed.\n", s.name);
return 0;
}
Explanation
Structure members can be used directly in conditions. Keeping
student information together makes record-based decision making easier.
Expected Output
Enter roll number: 105
Enter name: Neha
Enter marks: 67
Neha has passed.
Advantages of Structures — Exercise 7
Problem
Write a C program to store product name, price and quantity in a structure
and calculate the total bill amount.
Program
#include <stdio.h>
struct Product
{
char name[50];
float price;
int quantity;
};
int main()
{
struct Product p;
float total;
printf("Enter product name: ");
scanf("%49s", p.name);
printf("Enter price: ");
scanf("%f", &p.price);
printf("Enter quantity: ");
scanf("%d", &p.quantity);
// Calculate total amount
total = p.price * p.quantity;
printf("\nProduct = %s\n", p.name);
printf("Total Bill = %.2f\n", total);
return 0;
}
Explanation
A structure can represent a complete real-world entity. Here,
product details are stored together and used directly to calculate
the bill amount.
Expected Output
Enter product name: Keyboard
Enter price: 800
Enter quantity: 3
Product = Keyboard
Total Bill = 2400.00
Advantages of Structures — Exercise 8
Problem
Write a C program to create two employee records using a structure
and calculate the total of their salaries.
Program
#include <stdio.h>
struct Employee
{
int id;
char name[50];
float salary;
};
int main()
{
struct Employee e1, e2;
float totalSalary;
printf("Enter first employee ID, name and salary: ");
scanf("%d %49s %f",
&e1.id,
e1.name,
&e1.salary);
printf("Enter second employee ID, name and salary: ");
scanf("%d %49s %f",
&e2.id,
e2.name,
&e2.salary);
// Add salaries from both records
totalSalary = e1.salary + e2.salary;
printf("Total Salary = %.2f\n", totalSalary);
return 0;
}
Explanation
Structures allow multiple related records to be maintained using
variables of the same structure type. This makes record management
more organized.
Expected Output
Enter first employee ID, name and salary: 101 Amit 40000
Enter second employee ID, name and salary: 102 Neha 45000
Total Salary = 85000.00
Advantages of Structures — Exercise 9
Problem
Write a C program to store the date of birth of a person using a
structure and calculate the person's approximate age from the birth year.
Program
#include <stdio.h>
struct Date
{
int day;
int month;
int year;
};
int main()
{
struct Date dob;
int currentYear, age;
printf("Enter date of birth (day month year): ");
scanf("%d %d %d",
&dob.day,
&dob.month,
&dob.year);
printf("Enter current year: ");
scanf("%d", ¤tYear);
// Calculate approximate age
age = currentYear - dob.year;
printf("Date of Birth = %02d/%02d/%d\n",
dob.day,
dob.month,
dob.year);
printf("Approximate age = %d years\n", age);
return 0;
}
Explanation
The date is naturally represented as a group of related values.
A structure keeps day, month and year together as a single logical record.
Expected Output
Enter date of birth (day month year): 15 8 2005
Enter current year: 2026
Date of Birth = 15/08/2005
Approximate age = 21 years
Advantages of Structures — Exercise 10
Problem
Write a C program to create a complete student record using a structure.
Store roll number, name and marks of three subjects, then calculate
total, average and display the result.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
int marks1;
int marks2;
int marks3;
};
int main()
{
struct Student s;
int total;
float average;
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter name: ");
scanf("%49s", s.name);
printf("Enter marks of three subjects: ");
scanf("%d %d %d",
&s.marks1,
&s.marks2,
&s.marks3);
// Calculate total and average
total = s.marks1 + s.marks2 + s.marks3;
average = total / 3.0;
printf("\n-------------------------\n");
printf(" STUDENT RECORD\n");
printf("-------------------------\n");
printf("Roll Number : %d\n", s.roll);
printf("Name : %s\n", s.name);
printf("Total Marks : %d\n", total);
printf("Average : %.2f\n", average);
printf("-------------------------\n");
return 0;
}
Explanation
This final example demonstrates the main advantage of structures:
different but related data can be represented as one complete record.
The record can then be easily read, processed and displayed.
Expected Output
Enter roll number: 101
Enter name: Rahul
Enter marks of three subjects: 70 80 90
-------------------------
STUDENT RECORD
-------------------------
Roll Number : 101
Name : Rahul
Total Marks : 240
Average : 80.00
-------------------------