Class 12: C Programming Lab Solutions - Complete Guide
C Programming Lab Solutions - Complete Guide
Note for Students: These solutions are provided for your reference and learning. Make sure you understand the logic behind each program before writing them in your copies.
Pointers (Questions 16-20)
16. Print value and memory location using pointer
#include <stdio.h>
int main() {
int num = 42;
int *ptr;
ptr = #
printf("Value of num: %d\n", num);
printf("Memory location of num: %p\n", (void*)&num);
printf("Value using pointer: %d\n", *ptr);
printf("Memory location using pointer: %p\n", (void*)ptr);
return 0;
}
17. Swap two numbers using call-by-value and call-by-reference
#include <stdio.h>
// Call by value - doesn't swap original values
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside swapByValue: a = %d, b = %d\n", a, b);
}
// Call by reference - swaps original values
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n\n", x, y);
printf("Using Call by Value:\n");
swapByValue(x, y);
printf("After swapByValue: x = %d, y = %d\n\n", x, y);
printf("Using Call by Reference:\n");
swapByReference(&x, &y);
printf("After swapByReference: x = %d, y = %d\n", x, y);
return 0;
}
18. Calculate Simple Interest using pointers
#include <stdio.h>
void calculateSI(float *p, float *r, float *t, float *si) {
*si = (*p * *r * *t) / 100;
}
int main() {
float principal, rate, time, simpleInterest;
printf("Enter Principal amount: ");
scanf("%f", &principal);
printf("Enter Rate of interest: ");
scanf("%f", &rate);
printf("Enter Time period (in years): ");
scanf("%f", &time);
calculateSI(&principal, &rate, &time, &simpleInterest);
printf("\nPrincipal: %.2f\n", principal);
printf("Rate: %.2f%%\n", rate);
printf("Time: %.2f years\n", time);
printf("Simple Interest: %.2f\n", simpleInterest);
return 0;
}
19. Calculate volume of cuboid using pointers
#include <stdio.h>
void calculateVolume(float *l, float *w, float *h, float *vol) {
*vol = (*l) * (*w) * (*h);
}
int main() {
float length, width, height, volume;
printf("Enter length of cuboid: ");
scanf("%f", &length);
printf("Enter width of cuboid: ");
scanf("%f", &width);
printf("Enter height of cuboid: ");
scanf("%f", &height);
calculateVolume(&length, &width, &height, &volume);
printf("\nDimensions: %.2f x %.2f x %.2f\n", length, width, height);
printf("Volume of cuboid: %.2f cubic units\n", volume);
return 0;
}
20. Reverse a number using pointers
#include <stdio.h>
void reverseNumber(int *num, int *rev) {
int n = *num;
*rev = 0;
while(n != 0) {
*rev = *rev * 10 + n % 10;
n = n / 10;
}
}
int main() {
int number, reversed;
printf("Enter a number: ");
scanf("%d", &number);
reverseNumber(&number, &reversed);
printf("Original number: %d\n", number);
printf("Reversed number: %d\n", reversed);
return 0;
}
Structure and Union (Questions 21-37)
21. Single student details using structure
#include <stdio.h>
struct Student {
int rollNo;
char name[50];
float marks;
};
int main() {
struct Student s;
printf("Enter Roll Number: ");
scanf("%d", &s.rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", s.name);
printf("Enter Marks: ");
scanf("%f", &s.marks);
printf("\n--- Student Details ---\n");
printf("Roll Number: %d\n", s.rollNo);
printf("Name: %s\n", s.name);
printf("Marks: %.2f\n", s.marks);
return 0;
}
22. 5 students details using structure
#include <stdio.h>
struct Student {
int rollNo;
char name[50];
float marks;
};
int main() {
struct Student s[5];
int i;
printf("Enter details of 5 students:\n\n");
for(i = 0; i < 5; i++) {
printf("Student %d:\n", i+1);
printf("Enter Roll Number: ");
scanf("%d", &s[i].rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", s[i].name);
printf("Enter Marks: ");
scanf("%f", &s[i].marks);
printf("\n");
}
printf("\n--- Details of All Students ---\n");
for(i = 0; i < 5; i++) {
printf("\nStudent %d:\n", i+1);
printf("Roll Number: %d\n", s[i].rollNo);
printf("Name: %s\n", s[i].name);
printf("Marks: %.2f\n", s[i].marks);
}
return 0;
}
23. N employees details using structure
#include <stdio.h>
struct Employee {
int empId;
char empName[50];
float salary;
};
int main() {
int n, i;
printf("Enter number of employees: ");
scanf("%d", &n);
struct Employee emp[n];
printf("\nEnter details of %d employees:\n\n", n);
for(i = 0; i < n; i++) {
printf("Employee %d:\n", i+1);
printf("Enter Employee ID: ");
scanf("%d", &emp[i].empId);
printf("Enter Employee Name: ");
scanf(" %[^\n]", emp[i].empName);
printf("Enter Salary: ");
scanf("%f", &emp[i].salary);
printf("\n");
}
printf("\n--- Details of All Employees ---\n");
for(i = 0; i < n; i++) {
printf("\nEmployee %d:\n", i+1);
printf("Employee ID: %d\n", emp[i].empId);
printf("Employee Name: %s\n", emp[i].empName);
printf("Salary: %.2f\n", emp[i].salary);
}
return 0;
}
24. 3 books details using structure
#include <stdio.h>
struct Book {
int bookId;
char title[100];
float price;
char isbn[20];
};
int main() {
struct Book b[3];
int i;
printf("Enter details of 3 books:\n\n");
for(i = 0; i < 3; i++) {
printf("Book %d:\n", i+1);
printf("Enter Book ID: ");
scanf("%d", &b[i].bookId);
printf("Enter Title: ");
scanf(" %[^\n]", b[i].title);
printf("Enter Price: ");
scanf("%f", &b[i].price);
printf("Enter ISBN: ");
scanf(" %[^\n]", b[i].isbn);
printf("\n");
}
printf("\n--- Details of All Books ---\n");
for(i = 0; i < 3; i++) {
printf("\nBook %d:\n", i+1);
printf("Book ID: %d\n", b[i].bookId);
printf("Title: %s\n", b[i].title);
printf("Price: %.2f\n", b[i].price);
printf("ISBN: %s\n", b[i].isbn);
}
return 0;
}
25. N products details using structure
#include <stdio.h>
struct Product {
int productId;
char productName[50];
float price;
};
int main() {
int n, i;
printf("Enter number of products: ");
scanf("%d", &n);
struct Product p[n];
printf("\nEnter details of %d products:\n\n", n);
for(i = 0; i < n; i++) {
printf("Product %d:\n", i+1);
printf("Enter Product ID: ");
scanf("%d", &p[i].productId);
printf("Enter Product Name: ");
scanf(" %[^\n]", p[i].productName);
printf("Enter Price: ");
scanf("%f", &p[i].price);
printf("\n");
}
printf("\n--- Details of All Products ---\n");
for(i = 0; i < n; i++) {
printf("\nProduct %d:\n", i+1);
printf("Product ID: %d\n", p[i].productId);
printf("Product Name: %s\n", p[i].productName);
printf("Price: %.2f\n", p[i].price);
}
return 0;
}
26. N bank accounts details using structure
#include <stdio.h>
struct BankAccount {
long accountNumber;
char accountHolderName[50];
char accountType[20];
float balance;
};
int main() {
int n, i;
printf("Enter number of bank accounts: ");
scanf("%d", &n);
struct BankAccount acc[n];
printf("\nEnter details of %d bank accounts:\n\n", n);
for(i = 0; i < n; i++) {
printf("Account %d:\n", i+1);
printf("Enter Account Number: ");
scanf("%ld", &acc[i].accountNumber);
printf("Enter Account Holder Name: ");
scanf(" %[^\n]", acc[i].accountHolderName);
printf("Enter Account Type (Savings/Current): ");
scanf(" %[^\n]", acc[i].accountType);
printf("Enter Balance: ");
scanf("%f", &acc[i].balance);
printf("\n");
}
printf("\n--- Details of All Bank Accounts ---\n");
for(i = 0; i < n; i++) {
printf("\nAccount %d:\n", i+1);
printf("Account Number: %ld\n", acc[i].accountNumber);
printf("Account Holder Name: %s\n", acc[i].accountHolderName);
printf("Account Type: %s\n", acc[i].accountType);
printf("Balance: %.2f\n", acc[i].balance);
}
return 0;
}
27. Citizens with age > 18 using structure
#include <stdio.h>
struct Citizen {
int citizenId;
char name[50];
int age;
};
int main() {
int n, i;
printf("Enter number of citizens: ");
scanf("%d", &n);
struct Citizen c[n];
printf("\nEnter details of %d citizens:\n\n", n);
for(i = 0; i < n; i++) {
printf("Citizen %d:\n", i+1);
printf("Enter Citizen ID: ");
scanf("%d", &c[i].citizenId);
printf("Enter Name: ");
scanf(" %[^\n]", c[i].name);
printf("Enter Age: ");
scanf("%d", &c[i].age);
printf("\n");
}
printf("\n--- Citizens with Age Greater Than 18 ---\n");
for(i = 0; i < n; i++) {
if(c[i].age > 18) {
printf("\nCitizen ID: %d\n", c[i].citizenId);
printf("Name: %s\n", c[i].name);
printf("Age: %d\n", c[i].age);
}
}
return 0;
}
28. Movies with rating equal to 8 using structure
#include <stdio.h>
struct Movie {
int movieId;
char title[100];
int rating;
};
int main() {
int n, i;
printf("Enter number of movies: ");
scanf("%d", &n);
struct Movie m[n];
printf("\nEnter details of %d movies:\n\n", n);
for(i = 0; i < n; i++) {
printf("Movie %d:\n", i+1);
printf("Enter Movie ID: ");
scanf("%d", &m[i].movieId);
printf("Enter Title: ");
scanf(" %[^\n]", m[i].title);
printf("Enter Rating (1-10): ");
scanf("%d", &m[i].rating);
printf("\n");
}
printf("\n--- Movies with Rating Equal to 8 ---\n");
for(i = 0; i < n; i++) {
if(m[i].rating == 8) {
printf("\nMovie ID: %d\n", m[i].movieId);
printf("Title: %s\n", m[i].title);
printf("Rating: %d\n", m[i].rating);
}
}
return 0;
}
29. Books with genre "Horror" using structure
#include <stdio.h>
#include <string.h>
struct Book {
char authorName[50];
char genre[30];
float price;
char isbn[20];
};
int main() {
int n, i;
printf("Enter number of books: ");
scanf("%d", &n);
struct Book b[n];
printf("\nEnter details of %d books:\n\n", n);
for(i = 0; i < n; i++) {
printf("Book %d:\n", i+1);
printf("Enter Author Name: ");
scanf(" %[^\n]", b[i].authorName);
printf("Enter Genre: ");
scanf(" %[^\n]", b[i].genre);
printf("Enter Price: ");
scanf("%f", &b[i].price);
printf("Enter ISBN: ");
scanf(" %[^\n]", b[i].isbn);
printf("\n");
}
printf("\n--- Books with Genre 'Horror' ---\n");
for(i = 0; i < n; i++) {
if(strcmp(b[i].genre, "Horror") == 0) {
printf("\nAuthor Name: %s\n", b[i].authorName);
printf("Genre: %s\n", b[i].genre);
printf("Price: %.2f\n", b[i].price);
printf("ISBN: %s\n", b[i].isbn);
}
}
return 0;
}
30. Employees in Finance department using structure
#include <stdio.h>
#include <string.h>
struct Employee {
char name[50];
char address[100];
char department[30];
float salary;
};
int main() {
int n, i;
printf("Enter number of employees: ");
scanf("%d", &n);
struct Employee emp[n];
printf("\nEnter details of %d employees:\n\n", n);
for(i = 0; i < n; i++) {
printf("Employee %d:\n", i+1);
printf("Enter Name: ");
scanf(" %[^\n]", emp[i].name);
printf("Enter Address: ");
scanf(" %[^\n]", emp[i].address);
printf("Enter Department: ");
scanf(" %[^\n]", emp[i].department);
printf("Enter Salary: ");
scanf("%f", &emp[i].salary);
printf("\n");
}
printf("\n--- Employees in Finance Department ---\n");
for(i = 0; i < n; i++) {
if(strcmp(emp[i].department, "Finance") == 0) {
printf("\nName: %s\n", emp[i].name);
printf("Address: %s\n", emp[i].address);
printf("Department: %s\n", emp[i].department);
printf("Salary: %.2f\n", emp[i].salary);
}
}
return 0;
}
31. Sort students by name in ascending order
#include <stdio.h>
#include <string.h>
struct Student {
int rollNo;
char name[50];
float marks;
};
int main() {
int n, i, j;
struct Student temp;
printf("Enter number of students: ");
scanf("%d", &n);
struct Student s[n];
printf("\nEnter details of %d students:\n\n", n);
for(i = 0; i < n; i++) {
printf("Student %d:\n", i+1);
printf("Enter Roll Number: ");
scanf("%d", &s[i].rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", s[i].name);
printf("Enter Marks: ");
scanf("%f", &s[i].marks);
printf("\n");
}
// Bubble sort by name
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(strcmp(s[j].name, s[j+1].name) > 0) {
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
printf("\n--- Students Sorted by Name (Ascending) ---\n");
for(i = 0; i < n; i++) {
printf("\nRoll Number: %d\n", s[i].rollNo);
printf("Name: %s\n", s[i].name);
printf("Marks: %.2f\n", s[i].marks);
}
return 0;
}
32. Sort employees by salary in descending order
#include <stdio.h>
struct Employee {
int empId;
char empName[50];
float salary;
};
int main() {
int n, i, j;
struct Employee temp;
printf("Enter number of employees: ");
scanf("%d", &n);
struct Employee emp[n];
printf("\nEnter details of %d employees:\n\n", n);
for(i = 0; i < n; i++) {
printf("Employee %d:\n", i+1);
printf("Enter Employee ID: ");
scanf("%d", &emp[i].empId);
printf("Enter Employee Name: ");
scanf(" %[^\n]", emp[i].empName);
printf("Enter Salary: ");
scanf("%f", &emp[i].salary);
printf("\n");
}
// Bubble sort by salary (descending)
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(emp[j].salary < emp[j+1].salary) {
temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
printf("\n--- Employees Sorted by Salary (Descending) ---\n");
for(i = 0; i < n; i++) {
printf("\nEmployee ID: %d\n", emp[i].empId);
printf("Employee Name: %s\n", emp[i].empName);
printf("Salary: %.2f\n", emp[i].salary);
}
return 0;
}
33. Product with 3rd highest price using structure
#include <stdio.h>
struct Product {
char productName[50];
char brand[30];
float price;
int quantity;
};
int main() {
int n, i, j;
struct Product temp;
printf("Enter number of products: ");
scanf("%d", &n);
struct Product p[n];
printf("\nEnter details of %d products:\n\n", n);
for(i = 0; i < n; i++) {
printf("Product %d:\n", i+1);
printf("Enter Product Name: ");
scanf(" %[^\n]", p[i].productName);
printf("Enter Brand: ");
scanf(" %[^\n]", p[i].brand);
printf("Enter Price: ");
scanf("%f", &p[i].price);
printf("Enter Quantity: ");
scanf("%d", &p[i].quantity);
printf("\n");
}
// Bubble sort by price (descending)
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(p[j].price < p[j+1].price) {
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
if(n >= 3) {
printf("\n--- Product with 3rd Highest Price ---\n");
printf("Product Name: %s\n", p[2].productName);
printf("Brand: %s\n", p[2].brand);
printf("Price: %.2f\n", p[2].price);
printf("Quantity: %d\n", p[2].quantity);
} else {
printf("Not enough products to find 3rd highest price.\n");
}
return 0;
}
34. Bank accounts with holder name starting with 'C'
#include <stdio.h>
struct BankAccount {
long account_no;
char holder_name[50];
float balance;
char account_type[20];
};
int main() {
int n, i;
printf("Enter number of bank accounts: ");
scanf("%d", &n);
struct BankAccount acc[n];
printf("\nEnter details of %d bank accounts:\n\n", n);
for(i = 0; i < n; i++) {
printf("Account %d:\n", i+1);
printf("Enter Account Number: ");
scanf("%ld", &acc[i].account_no);
printf("Enter Holder Name: ");
scanf(" %[^\n]", acc[i].holder_name);
printf("Enter Balance: ");
scanf("%f", &acc[i].balance);
printf("Enter Account Type: ");
scanf(" %[^\n]", acc[i].account_type);
printf("\n");
}
printf("\n--- Accounts with Holder Name Starting with 'C' ---\n");
for(i = 0; i < n; i++) {
if(acc[i].holder_name[0] == 'C' || acc[i].holder_name[0] == 'c') {
printf("\nAccount Number: %ld\n", acc[i].account_no);
printf("Holder Name: %s\n", acc[i].holder_name);
printf("Balance: %.2f\n", acc[i].balance);
printf("Account Type: %s\n", acc[i].account_type);
}
}
return 0;
}
35. Nested structure - Student with Address
#include <stdio.h>
struct Address {
int houseNo;
char street[50];
char city[30];
};
struct Student {
int rollNo;
char name[50];
float marks;
struct Address addr;
};
int main() {
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);
struct Student s[n];
printf("\nEnter details of %d students:\n\n", n);
for(i = 0; i < n; i++) {
printf("Student %d:\n", i+1);
printf("Enter Roll Number: ");
scanf("%d", &s[i].rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", s[i].name);
printf("Enter Marks: ");
scanf("%f", &s[i].marks);
printf("Enter House Number: ");
scanf("%d", &s[i].addr.houseNo);
printf("Enter Street: ");
scanf(" %[^\n]", s[i].addr.street);
printf("Enter City: ");
scanf(" %[^\n]", s[i].addr.city);
printf("\n");
}
printf("\n--- Student Details with Address ---\n");
for(i = 0; i < n; i++) {
printf("\nStudent %d:\n", i+1);
printf("Roll Number: %d\n", s[i].rollNo);
printf("Name: %s\n", s[i].name);
printf("Marks: %.2f\n", s[i].marks);
printf("Address: House No. %d, %s, %s\n",
s[i].addr.houseNo, s[i].addr.street, s[i].addr.city);
}
return 0;
}
36. Union with different data types
#include <stdio.h>
union Data {
int intValue;
float floatValue;
char charValue;
};
int main() {
union Data d;
printf("Size of union: %lu bytes\n\n", sizeof(d));
d.intValue = 42;
printf("Storing integer value:\n");
printf("Integer value: %d\n\n", d.intValue);
d.floatValue = 3.14;
printf("Storing float value (overwrites previous):\n");
printf("Float value: %.2f\n", d.floatValue);
printf("Integer value (corrupted): %d\n\n", d.intValue);
d.charValue = 'A';
printf("Storing char value (overwrites previous):\n");
printf("Char value: %c\n", d.charValue);
printf("Integer value (corrupted): %d\n", d.intValue);
printf("Float value (corrupted): %.2f\n\n", d.floatValue);
printf("Note: Union shares same memory for all members.\n");
printf("Only one member can hold a valid value at a time.\n");
return 0;
}
37. Union for 10 employees
#include <stdio.h>
union Employee {
int empId;
float salary;
char grade;
};
int main() {
union Employee emp[10];
int i;
printf("Enter Employee IDs for 10 employees:\n");
for(i = 0; i < 10; i++) {
printf("Employee %d ID: ", i+1);
scanf("%d", &emp[i].empId);
}
printf("\n--- Employee IDs ---\n");
for(i = 0; i < 10; i++) {
printf("Employee %d: %d\n", i+1, emp[i].empId);
}
printf("\nEnter Salaries for 10 employees:\n");
for(i = 0; i < 10; i++) {
printf("Employee %d Salary: ", i+1);
scanf("%f", &emp[i].salary);
}
printf("\n--- Employee Salaries ---\n");
for(i = 0; i < 10; i++) {
printf("Employee %d: %.2f\n", i+1, emp[i].salary);
}
printf("\nEnter Grades for 10 employees:\n");
for(i = 0; i < 10; i++) {
printf("Employee %d Grade: ", i+1);
scanf(" %c", &emp[i].grade);
}
printf("\n--- Employee Grades ---\n");
for(i = 0; i < 10; i++) {
printf("Employee %d: %c\n", i+1, emp[i].grade);
}
printf("\nNote: Each union element can store only one value at a time.\n");
return 0;
}
File Handling (Questions 38-44)
38. File handling with putc(), getc(), putw(), getw()
#include <stdio.h>
int main() {
FILE *fp;
char ch;
int num;
// Writing to file
fp = fopen("data.txt", "w");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
// Writing characters using putc()
putc('H', fp);
putc('e', fp);
putc('l', fp);
putc('l', fp);
putc('o', fp);
putc('\n', fp);
fclose(fp);
// Writing integers using putw()
fp = fopen("numbers.txt", "w");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
putw(100, fp);
putw(200, fp);
putw(300, fp);
fclose(fp);
// Reading characters using getc()
printf("Reading characters from file:\n");
fp = fopen("data.txt", "r");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
while((ch = getc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
// Reading integers using getw()
printf("\nReading integers from file:\n");
fp = fopen("numbers.txt", "r");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
while((num = getw(fp)) != EOF) {
printf("%d ", num);
}
printf("\n");
fclose(fp);
return 0;
}
39. Store and read student details from file
#include <stdio.h>
struct Student {
int rollNo;
char name[50];
float marks;
};
int main() {
FILE *fp;
struct Student s;
int n, i;
// Writing to file
fp = fopen("students.dat", "wb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter number of students: ");
scanf("%d", &n);
printf("\nEnter student details:\n");
for(i = 0; i < n; i++) {
printf("\nStudent %d:\n", i+1);
printf("Roll Number: ");
scanf("%d", &s.rollNo);
printf("Name: ");
scanf(" %[^\n]", s.name);
printf("Marks: ");
scanf("%f", &s.marks);
fwrite(&s, sizeof(struct Student), 1, fp);
}
fclose(fp);
printf("\nStudent records saved successfully!\n");
// Reading from file
fp = fopen("students.dat", "rb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("\n--- Student Records from File ---\n");
i = 1;
while(fread(&s, sizeof(struct Student), 1, fp) == 1) {
printf("\nStudent %d:\n", i++);
printf("Roll Number: %d\n", s.rollNo);
printf("Name: %s\n", s.name);
printf("Marks: %.2f\n", s.marks);
}
fclose(fp);
return 0;
}
40. Store and read employee details from file
#include <stdio.h>
struct Employee {
int empId;
char name[50];
float salary;
};
int main() {
FILE *fp;
struct Employee emp;
int n, i;
// Writing to file
fp = fopen("employees.dat", "wb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter number of employees: ");
scanf("%d", &n);
printf("\nEnter employee details:\n");
for(i = 0; i < n; i++) {
printf("\nEmployee %d:\n", i+1);
printf("Employee ID: ");
scanf("%d", &emp.empId);
printf("Name: ");
scanf(" %[^\n]", emp.name);
printf("Salary: ");
scanf("%f", &emp.salary);
fwrite(&emp, sizeof(struct Employee), 1, fp);
}
fclose(fp);
printf("\nEmployee records saved successfully!\n");
// Reading from file
fp = fopen("employees.dat", "rb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("\n--- Employee Records from File ---\n");
i = 1;
while(fread(&emp, sizeof(struct Employee), 1, fp) == 1) {
printf("\nEmployee %d:\n", i++);
printf("Employee ID: %d\n", emp.empId);
printf("Name: %s\n", emp.name);
printf("Salary: %.2f\n", emp.salary);
}
fclose(fp);
return 0;
}
41. Store 5 books and display books with price > 500
#include <stdio.h>
struct Book {
int bookId;
char title[100];
float price;
};
int main() {
FILE *fp;
struct Book b;
int i;
// Writing to file
fp = fopen("books.dat", "wb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter details of 5 books:\n");
for(i = 0; i < 5; i++) {
printf("\nBook %d:\n", i+1);
printf("Book ID: ");
scanf("%d", &b.bookId);
printf("Title: ");
scanf(" %[^\n]", b.title);
printf("Price: ");
scanf("%f", &b.price);
fwrite(&b, sizeof(struct Book), 1, fp);
}
fclose(fp);
printf("\nBook records saved successfully!\n");
// Reading from file and filtering
fp = fopen("books.dat", "rb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("\n--- Books with Price Greater Than 500 ---\n");
while(fread(&b, sizeof(struct Book), 1, fp) == 1) {
if(b.price > 500) {
printf("\nBook ID: %d\n", b.bookId);
printf("Title: %s\n", b.title);
printf("Price: %.2f\n", b.price);
}
}
fclose(fp);
return 0;
}
42. Store products and display Nike products
#include <stdio.h>
#include <string.h>
struct Product {
int productId;
char productName[50];
char brand[30];
float price;
};
int main() {
FILE *fp;
struct Product p;
int n, i;
// Writing to file
fp = fopen("products.dat", "wb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter number of products: ");
scanf("%d", &n);
printf("\nEnter product details:\n");
for(i = 0; i < n; i++) {
printf("\nProduct %d:\n", i+1);
printf("Product ID: ");
scanf("%d", &p.productId);
printf("Product Name: ");
scanf(" %[^\n]", p.productName);
printf("Brand: ");
scanf(" %[^\n]", p.brand);
printf("Price: ");
scanf("%f", &p.price);
fwrite(&p, sizeof(struct Product), 1, fp);
}
fclose(fp);
printf("\nProduct records saved successfully!\n");
// Reading from file and filtering
fp = fopen("products.dat", "rb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("\n--- Nike Products ---\n");
while(fread(&p, sizeof(struct Product), 1, fp) == 1) {
if(strcmp(p.brand, "Nike") == 0) {
printf("\nProduct ID: %d\n", p.productId);
printf("Product Name: %s\n", p.productName);
printf("Brand: %s\n", p.brand);
printf("Price: %.2f\n", p.price);
}
}
fclose(fp);
return 0;
}
43. Store and read bank account details from file
#include <stdio.h>
struct BankAccount {
long accountNumber;
char accountHolderName[50];
float balance;
};
int main() {
FILE *fp;
struct BankAccount acc;
int n, i;
// Writing to file
fp = fopen("accounts.dat", "wb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter number of bank accounts: ");
scanf("%d", &n);
printf("\nEnter bank account details:\n");
for(i = 0; i < n; i++) {
printf("\nAccount %d:\n", i+1);
printf("Account Number: ");
scanf("%ld", &acc.accountNumber);
printf("Account Holder Name: ");
scanf(" %[^\n]", acc.accountHolderName);
printf("Balance: ");
scanf("%f", &acc.balance);
fwrite(&acc, sizeof(struct BankAccount), 1, fp);
}
fclose(fp);
printf("\nBank account records saved successfully!\n");
// Reading from file
fp = fopen("accounts.dat", "rb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("\n--- Bank Account Records from File ---\n");
i = 1;
while(fread(&acc, sizeof(struct BankAccount), 1, fp) == 1) {
printf("\nAccount %d:\n", i++);
printf("Account Number: %ld\n", acc.accountNumber);
printf("Account Holder Name: %s\n", acc.accountHolderName);
printf("Balance: %.2f\n", acc.balance);
}
fclose(fp);
return 0;
}
44. Store and read N movie details from file
#include <stdio.h>
struct Movie {
int movieId;
char title[100];
int rating;
};
int main() {
FILE *fp;
struct Movie m;
int n, i;
// Writing to file
fp = fopen("movies.dat", "wb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter number of movies: ");
scanf("%d", &n);
printf("\nEnter movie details:\n");
for(i = 0; i < n; i++) {
printf("\nMovie %d:\n", i+1);
printf("Movie ID: ");
scanf("%d", &m.movieId);
printf("Title: ");
scanf(" %[^\n]", m.title);
printf("Rating (1-10): ");
scanf("%d", &m.rating);
fwrite(&m, sizeof(struct Movie), 1, fp);
}
fclose(fp);
printf("\nMovie records saved successfully!\n");
// Reading from file
fp = fopen("movies.dat", "rb");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("\n--- Movie Records from File ---\n");
i = 1;
while(fread(&m, sizeof(struct Movie), 1, fp) == 1) {
printf("\nMovie %d:\n", i++);
printf("Movie ID: %d\n", m.movieId);
printf("Title: %s\n", m.title);
printf("Rating: %d/10\n", m.rating);
}
fclose(fp);
return 0;
}
Important Notes for Students:
- Make sure to understand the logic of each program before copying.
- Memory addresses in pointer programs will vary each time you run them.
- For file handling programs, ensure you have write permissions in the directory.
- Compile programs using:
gcc program_name.c -o program_name - Run programs using:
./program_name(Linux/Mac) orprogram_name.exe(Windows) - Practice modifying these programs to strengthen your understanding.
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...