Basic Structure Programming Questions

Here are 10 C programming exercises that involve structures:

  1. Student Database: Create a structure to represent a student with fields like name, roll number, and marks in different subjects. Write functions to input and display student details.

  2. Employee Management System: Develop a program to manage employee records using structures. Include fields like employee ID, name, salary, and designation. Implement functions to add, delete, and display employee details.

  3. Book Inventory: Build a program that uses a structure to represent a book with attributes such as title, author, and price. Write functions to add books to an inventory and display the book details.

  4. Bank Account Management: Create a structure to represent a bank account with fields like account number, account holder name, and balance. Implement functions for deposit, withdrawal, and displaying account details.

  5. Rectangle Operations: Define a structure to represent a rectangle with attributes like length and width. Write functions to calculate the area and perimeter of the rectangle.

  6. Date Validation: Develop a program that uses a structure to represent a date (day, month, year). Implement a function to validate whether the entered date is valid or not.

  7. Time Conversion: Create a structure to represent time (hours, minutes, seconds). Write functions to convert time from 12-hour format to 24-hour format and vice versa.

  8. Point in Space: Define a structure to represent a point in three-dimensional space (x, y, z). Write functions to calculate the distance between two points and the midpoint of a line segment.

  9. Temperature Conversion: Build a program that uses a structure to represent temperature in Celsius and Fahrenheit. Implement functions to convert temperatures between the two scales.

  10. Address Book: Design a program that maintains an address book using structures. Include fields like name, address, and phone number. Implement functions to add, delete, and search for contacts in the address book.

These exercises cover a range of struct-related concepts and will help you practice and strengthen your skills in C programming. I have solved all of the above with explanatory comments. You should first make a sincere attempt at solving these problems before looking at the solutions.

  1. Student Database:

     #include <stdio.h>
    
     // Structure to represent a student
     struct Student {
         char name[50];
         int rollNumber;
         float marks;
     };
    
     // Function to input student details
     void inputStudent(struct Student *s) {
         printf("Enter name: ");
         scanf("%s", s->name);
         printf("Enter roll number: ");
         scanf("%d", &s->rollNumber);
         printf("Enter marks: ");
         scanf("%f", &s->marks);
     }
    
     // Function to display student details
     void displayStudent(struct Student s) {
         printf("Name: %s\nRoll Number: %d\nMarks: %.2f\n", s.name, s.rollNumber, s.marks);
     }
    
     int main() {
         // Create a student
         struct Student student1;
    
         // Input and display student details
         inputStudent(&student1);
         displayStudent(student1);
    
         return 0;
     }
    
  2. Employee Management System:

     #include <stdio.h>
    
     // Structure to represent an employee
     struct Employee {
         int employeeID;
         char name[50];
         float salary;
         char designation[50];
     };
    
     // Function to display employee details
     void displayEmployee(struct Employee e) {
         printf("Employee ID: %d\nName: %s\nSalary: %.2f\nDesignation: %s\n", e.employeeID, e.name, e.salary, e.designation);
     }
    
     int main() {
         // Create an employee
         struct Employee employee1 = {101, "John Doe", 50000.0, "Software Engineer"};
    
         // Display employee details
         displayEmployee(employee1);
    
         return 0;
     }
    
  3. Book Inventory:

     #include <stdio.h>
    
     // Structure to represent a book
     struct Book {
         char title[100];
         char author[50];
         float price;
     };
    
     // Function to display book details
     void displayBook(struct Book b) {
         printf("Title: %s\nAuthor: %s\nPrice: %.2f\n", b.title, b.author, b.price);
     }
    
     int main() {
         // Create a book
         struct Book book1 = {"The Catcher in the Rye", "J.D. Salinger", 12.99};
    
         // Display book details
         displayBook(book1);
    
         return 0;
     }
    
  4. Bank Account Management:

     #include <stdio.h>
    
     // Structure to represent a bank account
     struct BankAccount {
         int accountNumber;
         char accountHolderName[50];
         float balance;
     };
    
     // Function to display account details
     void displayAccount(struct BankAccount acc) {
         printf("Account Number: %d\nAccount Holder: %s\nBalance: %.2f\n", acc.accountNumber, acc.accountHolderName, acc.balance);
     }
    
     int main() {
         // Create a bank account
         struct BankAccount account1 = {1001, "Alice Johnson", 5000.0};
    
         // Display account details
         displayAccount(account1);
    
         return 0;
     }
    
  5. Rectangle Operations:

     #include <stdio.h>
    
     // Structure to represent a rectangle
     struct Rectangle {
         float length;
         float width;
     };
    
     // Function to calculate area of rectangle
     float calculateArea(struct Rectangle r) {
         return r.length * r.width;
     }
    
     // Function to calculate perimeter of rectangle
     float calculatePerimeter(struct Rectangle r) {
         return 2 * (r.length + r.width);
     }
    
     int main() {
         // Create a rectangle
         struct Rectangle rectangle1 = {5.0, 3.0};
    
         // Calculate and display area and perimeter
         printf("Area: %.2f\n", calculateArea(rectangle1));
         printf("Perimeter: %.2f\n", calculatePerimeter(rectangle1));
    
         return 0;
     }
    
  6. Date Validation:

     #include <stdio.h>
    
     // Structure to represent a date
     struct Date {
         int day;
         int month;
         int year;
     };
    
     // Function to validate date
     int isValidDate(struct Date d) {
         // Simple validation (ignoring leap years, etc.)
         return (d.day >= 1 && d.day <= 31 && d.month >= 1 && d.month <= 12 && d.year >= 1900);
     }
    
     int main() {
         // Create a date
         struct Date date1 = {15, 8, 2023};
    
         // Validate and display result
         if (isValidDate(date1)) {
             printf("Valid date.\n");
         } else {
             printf("Invalid date.\n");
         }
    
         return 0;
     }
    
  7. Time Conversion:

     #include <stdio.h>
    
     // Structure to represent time
     struct Time {
         int hours;
         int minutes;
         int seconds;
     };
    
     // Function to convert time to 24-hour format
     struct Time convertTo24Hour(struct Time t12) {
         // Assume t12 is in 12-hour format
         if (t12.hours == 12) {
             t12.hours = 0;
         }
    
         if (t12.seconds >= 0 && t12.seconds <= 59 && t12.minutes >= 0 && t12.minutes <= 59 && t12.hours >= 0 && t12.hours <= 11) {
             // AM
             return t12;
         } else if (t12.seconds >= 0 && t12.seconds <= 59 && t12.minutes >= 0 && t12.minutes <= 59 && t12.hours >= 12 && t12.hours <= 23) {
             // PM
             t12.hours = t12.hours - 12;
             return t12;
         } else {
             // Invalid time
             struct Time invalidTime = {-1, -1, -1};
             return invalidTime;
         }
     }
    
     int main() {
         // Create a time in 12-hour format
         struct Time time12 = {3, 45, 30}; // 3:45:30 AM
    
         // Convert and display time in 24-hour format
         struct Time time24 = convertTo24Hour(time12);
         if (time24.hours != -1) {
             printf("Time in 24-hour format: %02d:%02d:%02d\n", time24.hours, time24.minutes, time24.seconds);
         } else {
             printf("Invalid time.\n");
         }
    
         return 0;
     }
    
  8. Point in Space:

     #include <stdio.h>
     #include <math.h>
    
     // Structure to represent a point in 3D space
     struct Point {
         float x;
         float y;
         float z;
     };
    
     // Function to calculate distance between two points
     float calculateDistance(struct Point p1, struct Point p2) {
         return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2) + pow(p2.z - p1.z, 2));
     }
    
     // Function to calculate midpoint of a line segment
     struct Point calculateMidpoint(struct Point p1, struct Point p2) {
         struct Point midpoint;
         midpoint.x = (p1.x + p2.x) / 2;
         midpoint.y = (p1.y + p2.y) / 2;
         midpoint.z = (p1.z + p2.z) / 2;
         return midpoint;
     }
    
     int main() {
         // Create two points
         struct Point point1 = {1.0, 2.0, 3.0};
         struct Point point2 = {4.0, 5.0, 6.0};
    
         // Calculate and display distance and midpoint
         printf("Distance: %.2f\n", calculateDistance(point1, point2));
         struct Point midpoint = calculateMidpoint(point1, point2);
         printf("Midpoint: (%.2f, %.2f, %.2f)\n", midpoint.x, midpoint.y, midpoint.z);
    
         return 0;
     }
    
  9. Temperature Conversion:

     #include <stdio.h>
    
     // Structure to represent temperature
     struct Temperature {
         float celsius;
         float fahrenheit;
     };
    
     // Function to convert temperature from Celsius to Fahrenheit
     float convertCtoF(float celsius) {
         return (celsius * 9 / 5) + 32;
     }
    
     // Function to convert temperature from Fahrenheit to Celsius
     float convertFtoC(float fahrenheit) {
         return (fahrenheit - 32) * 5 / 9;
     }
    
     int main() {
         // Create a temperature in Celsius
         struct Temperature tempC = {25.0, convertCtoF(25.0)};
    
         // Display temperature in Fahrenheit
         printf("Temperature in Fahrenheit: %.2f\n", tempC.fahrenheit);
    
         // Create a temperature in Fahrenheit
         struct Temperature tempF = {convertFtoC(98.6), 98.6};
    
         // Display temperature in Celsius
         printf("Temperature in Celsius: %.2f\n", tempF.celsius);
    
         return 0;
     }
    
  10. Address Book:

    #include <stdio.h>
    
    // Structure to represent a contact
    struct Contact {
        char name[50];
        char address[100];
        char phoneNumber[15];
    };
    
    // Function to display contact details
    void displayContact(struct Contact c) {
        printf("Name: %s\nAddress: %s\nPhone Number: %s\n", c.name, c.address, c.phoneNumber);
    }
    
    int main() {
        // Create a contact
        struct Contact contact1 = {"John Doe", "123 Main St, Cityville", "555-1234"};
    
        // Display contact details
        displayContact(contact1);
    
        return 0;
    }
    
0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.