C Programming • Structure and Union
C Programming / Introduction to Structures — Exercises

Introduction to Structures — Exercises

Practical 4 Structure and Union

Write a C program to define a structure named Student containing roll number, name and marks. Read and display the student details.

Practical / Solution

Structures — Exercise 1

Problem

Write a C program to define a structure named Student containing roll number, name and marks. Read and display the student details.

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 student details printf("\nRoll Number = %d\n", s.roll); printf("Name = %s\n", s.name); printf("Marks = %.2f\n", s.marks); return 0; }

Explanation

A structure is a user-defined data type used to group related variables, even when those variables have different data types.

Expected Output

Enter roll number: 101

Enter name: Rahul

Enter marks: 82.5

Roll Number = 101

Name = Rahul

Marks = 82.50

Structures — Exercise 2

Problem

Write a C program to define an Employee structure containing employee ID, name and salary and display its members.

Program

#include <stdio.h> struct Employee { int id; char name[50]; float salary; }; int main() { struct Employee emp; printf("Enter employee ID: "); scanf("%d", &emp.id); printf("Enter name: "); scanf("%49s", emp.name); printf("Enter salary: "); scanf("%f", &emp.salary); // Display employee details printf("\nEmployee ID = %d\n", emp.id); printf("Name = %s\n", emp.name); printf("Salary = %.2f\n", emp.salary); return 0; }

Explanation

Each structure variable contains its own copy of all the members declared in the structure. Members are accessed using the dot operator.

Expected Output

Enter employee ID: 501

Enter name: Amit

Enter salary: 45000

Employee ID = 501

Name = Amit

Salary = 45000.00

Structures — Exercise 3

Problem

Write a C program to initialize a structure variable directly at the time of declaration and display its values.

Program

#include <stdio.h> struct Product { int id; char name[50]; float price; }; int main() { // Initialize structure during declaration struct Product p = {101, "Laptop", 55000.0}; printf("Product ID = %d\n", p.id); printf("Product Name = %s\n", p.name); printf("Price = %.2f\n", p.price); return 0; }

Explanation

A structure variable can be initialized at the time of declaration. The values are assigned to the members in their declared order.

Expected Output

Product ID = 101

Product Name = Laptop

Price = 55000.00

Structures — Exercise 4

Problem

Write a C program to store the dimensions of a rectangle in a structure and calculate its area and perimeter.

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); // Calculate area and perimeter area = r.length * r.width; perimeter = 2 * (r.length + r.width); printf("Area = %.2f\n", area); printf("Perimeter = %.2f\n", perimeter); return 0; }

Explanation

Structure members can participate in arithmetic expressions just like ordinary variables.

Expected Output

Enter length and width: 10 5

Area = 50.00

Perimeter = 30.00

Structures — Exercise 5

Problem

Write a C program to create two structure variables of type Student and display the details of both students.

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); // Display both records printf("\nStudent 1:\n"); printf("Roll = %d\n", s1.roll); printf("Name = %s\n", s1.name); printf("Marks = %.2f\n", s1.marks); printf("\nStudent 2:\n"); printf("Roll = %d\n", s2.roll); printf("Name = %s\n", s2.name); printf("Marks = %.2f\n", s2.marks); return 0; }

Explanation

Multiple structure variables of the same structure type can be created. Each variable stores an independent record.

Expected Output

Enter first student details: 101 Rahul 80

Enter second student details: 102 Priya 90

Student 1:

Roll = 101

Name = Rahul

Marks = 80.00

Student 2:

Roll = 102

Name = Priya

Marks = 90.00

Structures — Exercise 6

Problem

Write a C program to compare the marks of two students stored in structure variables and display the student with higher marks.

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 marks if (s1.marks > s2.marks) { printf("%s has higher marks.\n", s1.name); } else if (s2.marks > s1.marks) { printf("%s has higher marks.\n", s2.name); } else { printf("Both students have equal marks.\n"); } return 0; }

Explanation

Individual members of structure variables can be compared using ordinary relational operators.

Expected Output

Enter first student details: 101 Rahul 72

Enter second student details: 102 Priya 85

Priya has higher marks.

Structures — Exercise 7

Problem

Write a C program to store a time value using a structure containing hours, minutes and seconds, and display the time.

Program

#include <stdio.h> struct Time { int hours; int minutes; int seconds; }; int main() { struct Time t; printf("Enter hours, minutes and seconds: "); scanf("%d %d %d", &t.hours, &t.minutes, &t.seconds); // Display stored time printf("Time = %02d:%02d:%02d\n", t.hours, t.minutes, t.seconds); return 0; }

Explanation

A structure can represent a logical entity by grouping all of its related properties into one variable.

Expected Output

Enter hours, minutes and seconds: 14 30 25

Time = 14:30:25

Structures — Exercise 8

Problem

Write a C program to store the details of a bank account using a structure and calculate the balance after a deposit.

Program

#include <stdio.h> struct Account { int accountNo; char name[50]; float balance; }; int main() { struct Account acc; float deposit; printf("Enter account number: "); scanf("%d", &acc.accountNo); printf("Enter account holder name: "); scanf("%49s", acc.name); printf("Enter current balance: "); scanf("%f", &acc.balance); printf("Enter deposit amount: "); scanf("%f", &deposit); // Update the structure member acc.balance = acc.balance + deposit; printf("\nAccount Number = %d\n", acc.accountNo); printf("Name = %s\n", acc.name); printf("Updated Balance = %.2f\n", acc.balance); return 0; }

Explanation

A structure member can be updated directly using the dot operator. Here, the balance member is modified after a deposit.

Expected Output

Enter account number: 1001

Enter account holder name: Amit

Enter current balance: 25000

Enter deposit amount: 5000

Account Number = 1001

Name = Amit

Updated Balance = 30000.00

Structures — Exercise 9

Problem

Write a C program to store the details of a book and calculate its selling price after applying a discount.

Program

#include <stdio.h> struct Book { int id; char title[100]; float price; }; int main() { struct Book book; float discount, sellingPrice; 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 selling price sellingPrice = book.price - (book.price * discount / 100); printf("\nBook ID = %d\n", book.id); printf("Title = %s\n", book.title); printf("Selling Price = %.2f\n", sellingPrice); return 0; }

Explanation

Structure members can be used as inputs to formulas. The program calculates the final price using the price stored in the structure.

Expected Output

Enter book ID: 101

Enter title: CProgramming

Enter price: 500

Enter discount percentage: 10

Book ID = 101

Title = CProgramming

Selling Price = 450.00

Structures — Exercise 10

Problem

Write a C program to create a structure named Student containing roll number, name and marks of three subjects. Calculate total, average and display the complete student record.

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 exercise combines structure definition, member access and calculations. Related student information is kept together in one structure variable.

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

-------------------------