0% found this document useful (0 votes)
18 views11 pages

Assignment-1 A2

The document contains multiple C programming assignments that involve the use of structures and unions to manage data related to employees, customers, and books. Each assignment includes code snippets for defining structures, inputting data, and displaying information based on specific criteria. The document demonstrates various programming concepts such as data encapsulation, conditionals, and memory management.

Uploaded by

gatepsuair1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

Assignment-1 A2

The document contains multiple C programming assignments that involve the use of structures and unions to manage data related to employees, customers, and books. Each assignment includes code snippets for defining structures, inputting data, and displaying information based on specific criteria. The document demonstrates various programming concepts such as data encapsulation, conditionals, and memory management.

Uploaded by

gatepsuair1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

ASSIGNMENT -1

Q1

#include <stdio.h>

struct Employee {

int id;

char name[50];

char designation[50];

char department[50];

float salary;

};

int main() {

struct Employee emp;

printf("Enter Employee ID: ");

scanf("%d", &emp.id);

printf("Enter Employee Name: ");

scanf(" %[^\n]", emp.name);

printf("Enter Designation: ");

scanf(" %[^\n]", emp.designation);

printf("Enter Department: ");

scanf(" %[^\n]", emp.department);

printf("Enter Salary: ");

scanf("%f", &emp.salary);

printf("\nEmployee Details:\n");

printf("ID: %d\nName: %s\nDesignation: %s\nDepartment: %s\nSalary: %.2f\n",

emp.id, emp.name, emp.designation, emp.department, emp.salary);

return 0;

Q2

#include <stdio.h>

struct Customer {

int account_no;
char name[50];

float balance;

};

void print_low_balance(struct Customer customers[], int n) {

printf("\nCustomers with balance less than Rs. 100:\n");

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

if (customers[i].balance < 100) {

printf("Account No: %d, Name: %s\n", customers[i].account_no, customers[i].name);

int main() {

struct Customer customers[3];

for (int i = 0; i < 3; i++) {

printf("Enter details for customer %d:\n", i + 1);

printf("Account No: ");

scanf("%d", &customers[i].account_no);

printf("Name: ");

scanf(" %[^\n]", customers[i].name);

printf("Balance: ");

scanf("%f", &customers[i].balance);

print_low_balance(customers, 3);

return 0;

Q3

#include <stdio.h>

#include <string.h>

#define MAX 100

struct Book {

char title[50];
char author[50];

float cost;

};

struct Book library[MAX];

int count = 0;

void add_book() {

printf("Enter book title: ");

scanf(" %[^\n]", library[count].title);

printf("Enter author name: ");

scanf(" %[^\n]", library[count].author);

printf("Enter cost: ");

scanf("%f", &library[count].cost);

count++;

void display_books() {

printf("\nLibrary Books:\n");

for (int i = 0; i < count; i++) {

printf("Title: %s, Author: %s, Cost: %.2f\n", library[i].title, library[i].author, library[i].cost);

void books_by_author(char author[]) {

printf("\nBooks by %s:\n", author);

for (int i = 0; i < count; i++) {

if (strcmp(library[i].author, author) == 0) {

printf("Title: %s, Cost: %.2f\n", library[i].title, library[i].cost);

int main() {
int choice;

char author[50];

do {

printf("\nLibrary Menu:\n");

printf("1. Add Book\n2. Display Books\n3. Search by Author\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: add_book(); break;

case 2: display_books(); break;

case 3:

printf("Enter author name: ");

scanf(" %[^\n]", author);

books_by_author(author);

break;

case 4: printf("Exiting...\n"); break;

default: printf("Invalid choice!\n");

} while (choice != 4);

return 0;

Q4

#include <stdio.h>

#include <string.h>

#define MAX 100

struct Book {

int accession_number;

char title[50];

char author[50];

char publisher[50];

float cost;

};
void display_by_author(struct Book books[], int n, char author[]) {

printf("\nBooks by author %s:\n", author);

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

if (strcmp(books[i].author, author) == 0) {

printf("Accession Number: %d, Title: %s\n", books[i].accession_number, books[i].title);

void display_by_publisher(struct Book books[], int n, char publisher[]) {

printf("\nBooks by publisher %s:\n", publisher);

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

if (strcmp(books[i].publisher, publisher) == 0) {

printf("Accession Number: %d, Title: %s\n", books[i].accession_number, books[i].title);

void display_costly_books(struct Book books[], int n) {

printf("\nBooks costing Rs. 500 and above:\n");

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

if (books[i].cost >= 500) {

printf("Accession Number: %d, Title: %s, Cost: %.2f\n",

books[i].accession_number, books[i].title, books[i].cost);

void display_all_books(struct Book books[], int n) {

printf("\nAll Books:\n");

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

printf("Accession Number: %d, Title: %s, Author: %s, Publisher: %s, Cost: %.2f\n",

books[i].accession_number, books[i].title, books[i].author, books[i].publisher, books[i].cost);


}

int main() {

struct Book books[MAX];

int n;

printf("Enter the number of books: ");

scanf("%d", &n);

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

books[i].accession_number = i + 1;

printf("\nEnter details for book %d:\n", i + 1);

printf("Title: ");

scanf(" %[^\n]", books[i].title);

printf("Author: ");

scanf(" %[^\n]", books[i].author);

printf("Publisher: ");

scanf(" %[^\n]", books[i].publisher);

printf("Cost: ");

scanf("%f", &books[i].cost);

char author[50], publisher[50];

printf("\nEnter author to search: ");

scanf(" %[^\n]", author);

display_by_author(books, n, author);

printf("\nEnter publisher to search: ");

scanf(" %[^\n]", publisher);

display_by_publisher(books, n, publisher);

display_costly_books(books, n);

display_all_books(books, n);
return 0;

Q5

#include <stdio.h>

#include <string.h>

#define MAX 100

struct Employee {

int empid;

char name[50];

float salary;

};

void search_by_id(struct Employee employees[], int n, int id) {

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

if (employees[i].empid == id) {

printf("Employee Found: ID: %d, Name: %s, Salary: %.2f\n",

employees[i].empid, employees[i].name, employees[i].salary);

return;

printf("Employee with ID %d not found.\n", id);

void display_all(struct Employee employees[], int n) {

printf("\nAll Employees:\n");

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

printf("ID: %d, Name: %s, Salary: %.2f\n",

employees[i].empid, employees[i].name, employees[i].salary);

void display_high_salary(struct Employee employees[], int n) {


printf("\nEmployees with Salary > 10000:\n");

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

if (employees[i].salary > 10000) {

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

int main() {

struct Employee employees[MAX];

int n, choice, id;

printf("Enter the number of employees: ");

scanf("%d", &n);

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

printf("\nEnter details for employee %d:\n", i + 1);

printf("Employee ID: ");

scanf("%d", &employees[i].empid);

printf("Name: ");

scanf(" %[^\n]", employees[i].name);

printf("Salary: ");

scanf("%f", &employees[i].salary);

do {

printf("\nMenu:\n");

printf("1. Search by Emp-ID\n2. Display All Employees\n3. Display Employees with Salary > 10000\n4.
Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:
printf("Enter Employee ID to search: ");

scanf("%d", &id);

search_by_id(employees, n, id);

break;

case 2:

display_all(employees, n);

break;

case 3:

display_high_salary(employees, n);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice!\n");

} while (choice != 4);

return 0;

Q6

#include <stdio.h>

#include <string.h>

union Data {

int number;

char name[20];

};

int main() {

union Data data;

strcpy(data.name, "YourName"); // Replace with your name

printf("String stored: %s\n", data.name);

printf("Integer value: %d\n", data.number); // Analyze corruption in data


return 0;

Q7

#include <stdio.h>

union ByteExtractor {

unsigned int number;

unsigned char bytes[4];

};

int main() {

union ByteExtractor extractor;

printf("Enter an unsigned integer: ");

scanf("%u", &extractor.number);

printf("Bytes of the number:\n");

for (int i = 0; i < 4; i++) {

printf("Byte %d: %x\n", i, extractor.bytes[i]);

return 0;

Q8

#include <stdio.h>

union EndiannessCheck {

unsigned int number;

unsigned char byte;

};

int main() {

union EndiannessCheck check;

check.number = 1;

if (check.byte == 1) {
printf("The machine is Little Endian.\n");

} else {

printf("The machine is Big Endian.\n");

return 0;

You might also like