0% found this document useful (0 votes)
9 views

Lab Assignment 11

Uploaded by

pranjal15sh
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)
9 views

Lab Assignment 11

Uploaded by

pranjal15sh
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/ 18

LAB ASSIGNMENT 11

Q.) Code for Basic i/o of structures.

#include<stdio.h>
int main()
{
struct student
{
int rollno;
char name[10];
int marks;
};

struct student stud1;


struct student stud2;
printf("Enter the details of student 1:");
scanf("%d %s %d",&stud1.rollno,&stud1.name,&stud1.marks);
printf("Enter the details of student 2:");
scanf("%d %s %d",&stud2.rollno,&stud2.name,&stud2.marks);
printf("Details of Stud1:%d ,%s ,%d\
n",stud1.rollno,stud1.name,stud1.marks);
printf("Details of Stud2:%d ,%s ,
%d",stud2.rollno,stud2.name,stud2.marks);

return 0;
}
Output:

Q.) Array of structure i/o.

#include<stdio.h>
int main()
{

struct student
{
int rollno;
char name[10]; int marks;
}stud[3];
int i;
for(i=0;i<=2;i++)
{
printf("enter data for student %d:",i+1);
scanf("%d %s %d",&stud[i].rollno,&stud[i].name,&stud[i].marks);
}
for(i=0;i<=2;i++)
{
printf("data for student %d: ",i+1);
printf("%d %s %d\n",stud[i].rollno,stud[i].name,stud[i].marks);
}

Output:
Q.)Structure as a input of another structure i/o.

#include<stdio.h>
int main()
{

struct marks
{
int phy; int chem; int maths;
};
struct student
{
int rollno;
char name[10];
struct marks m;
}stud[3];

int i,sum=0,sum1=0,sum2=0;
for(i=0;i<=2;i++)
{
printf("enter data for student:");
scanf("%d %s",&stud[i].rollno,stud[i].name);
printf("marks in physics, chemistry and maths");
scanf("%d %d %d",&stud[i].m.phy,&stud[i].m.chem,&stud[i].m.maths);
}
for(i=0;i<=2;i++)
{
printf("\n %d %s %d %d
%d",stud[i].rollno,stud[i].name,stud[i].m.phy,stud[i].m.chem,stud[i].m.maths)
;
}

for(i=0;i<=2;i++)
{
sum=sum+stud[i].m.phy; sum1=sum1+stud[i].m.chem;
sum2=sum2+stud[i].m.maths;
}

printf("\n sum of marks of physics : %d",sum); printf("\n sum of marks of


chemistry : %d",sum1); printf("\n sum of marks of maths : %d",sum2);

Output.

Q1.)
#include <stdio.h>
#include <string.h>

struct student {
char rollno[10];
char name[50];
char department[8];
char course[20];
int year_of_joining;
}std[250];

void print_joining_year(struct student students[], int search) {


printf("Students who joined in the year %d:\n", search);
for (int i=0;i<250;i++){
if (students[i].year_of_joining == search) {
printf("%s\n", students[i].name);
}
}
}

void print_rollno(struct student students[], char search[]) {


printf("Student with roll number %s:\n", search);
for (int i=0;i<250;i++){
if (strcmp(students[i].rollno, search) == 0) {
printf("%s\n", students[i].name);
}
}
}

int main() {
struct student std[250] = {
{"241036001", "T", "CSE", "B.Tech", 2022},
{"241036002", "D", "CSE", "B.Tech", 2024},
{"241034012", "R", "CSE", "B.Tech", 2023},
{"241033030", "P", "CSE", "B.Tech", 2024},
{"241036003", "A", "CSE", "B.Tech", 2021},
{"241033030", "B", "CSE", "B.Tech", 2024},
{"241033037", "G", "CSE", "B.Tech", 2023},
{"241033034", "H", "CSE", "B.Tech", 2023},
{"241033031", "J", "CSE", "B.Tech", 2022},
{"241033033", "K", "CSE", "B.Tech", 2024}
};

int year;
char roll[10];

printf("Enter year to search: ");


scanf("%d",&year);
print_joining_year(std, year);

printf("Enter rollno to search: ");


scanf(" %[^\n]s",roll);
print_rollno(std, roll);

return 0;
}

Output.
Q2.)

#include <stdio.h>
#include <string.h>

#define MAX_CUSTOMERS 200

struct customer {
int account_number;
char name[50];
float balance;
};

void print_low_balance(struct customer customers[], int count) {


printf("\nCustomers with balance below Rs. 100:\n");
int found = 0;
for (int i = 0; i < count; i++) {
if (customers[i].balance < 100) {
printf("Account Number: %d, Name: %s, Balance: Rs. %.2f\n",
customers[i].account_number, customers[i].name,
customers[i].balance);
found = 1;
}
}
if (!found) {
printf("No customers with balance below Rs. 100.\n");
}
}

void handle_transaction(struct customer customers[], int count, int


acc_number, float amount, int code) {
for (int i = 0; i < count; i++) {
if (customers[i].account_number == acc_number) {
if (code == 1) {
customers[i].balance += amount;
printf("Deposit successful! New balance for Account %d: Rs.
%.2f\n", acc_number, customers[i].balance);
} else if (code == 0) {
if (customers[i].balance >= amount) {
customers[i].balance -= amount;
printf("Withdrawal successful! New balance for Account %d:
Rs. %.2f\n", acc_number, customers[i].balance);
} else {
printf("The balance is insufficient for the specified withdrawal.
Current balance: Rs. %.2f\n", customers[i].balance);
}
} else {
printf("Invalid transaction code.\n");
}
return;
}
}
printf("Account number %d not found.\n", acc_number);
}

int main() {
struct customer customers[MAX_CUSTOMERS];
int n, acc_number, code;
float amount;

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


scanf("%d", &n);

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


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

printf("Account Number: ");


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

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

printf("Balance: ");
scanf("%f", &customers[i].balance);
}

print_low_balance(customers, n);

printf("\nEnter transaction details (Account Number, Amount, Code [1 for


deposit, 0 for withdrawal]):\n");
scanf("%d %f %d", &acc_number, &amount, &code);
handle_transaction(customers, n, acc_number, amount, code);

return 0;
}
Q3.)

#include <stdio.h>
#include <string.h>

struct date {
int day;
int month;
int year;
};

struct employee {
int empcode;
char name[50];
struct date date_of_joining;
};

int calculate_tenure(struct date current_date, struct date joining_date) {


int years = current_date.year - joining_date.year;

return years;
}

int main() {
int n;
printf("Enter number of employees: ");
scanf("%d", &n);

struct employee employees[100];

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


printf("\nEnter details for employee %d:\n", i + 1);
printf("Employee Code: ");
scanf(" %d", &employees[i].empcode);

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

printf("Date of Joining (DD MM YYYY): ");


scanf(" %d %d %d", &employees[i].date_of_joining.day,
&employees[i].date_of_joining.month,
&employees[i].date_of_joining.year);
}

struct date current_date;


printf("\nEnter current date (DD MM YYYY): ");
scanf(" %d %d %d", &current_date.day, &current_date.month,
&current_date.year);

printf("\nEmployees with tenure of 3 or more years:\n");


int found = 0;
for (int i = 0; i < n; i++) {
int tenure = calculate_tenure(current_date,
employees[i].date_of_joining);
if (tenure >= 3) {
printf("Employee Code: %d, Name: %s, Tenure: %d years\n",
employees[i].empcode, employees[i].name, tenure);
found = 1;
}
}
if (!found) {
printf("No employees with tenure of 3 or more years.\n");
}
return 0;
}
Q4.)

#include <stdio.h>
#include <string.h>

#define MAX_BOOKS 100

struct library {
int accession_number;
char title[50];
char author[50];
float price;
int is_issued;
};

void add_book(struct library books[], int *count) {


printf("Enter accession number: ");
scanf("%d", &books[*count].accession_number);

printf("Enter title of the book: ");


scanf(" %[^\n]s",books[*count].title);

printf("Enter author name: ");


scanf(" %[^\n]s",books[*count].author);

printf("Enter price of the book: ");


scanf("%f", &books[*count].price);

printf("Is the book issued? (1 for Yes, 0 for No): ");


scanf("%d", &books[*count].is_issued);
(*count)++;
printf("Book added successfully!\n");
}

void display_books(struct library books[], int count) {


for (int i = 0; i < count; i++) {
printf("\nBook %d\n", i + 1);
printf("Accession Number: %d\n", books[i].accession_number);
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Price: %.2f\n", books[i].price);
printf("Issued: %s\n", books[i].is_issued ? "Yes" : "No");
}
}

void list_books_by_author(struct library books[], int count) {


char author[50];
printf("Enter author's name: ");
scanf(" %[^\n]s",author);

int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(books[i].author, author) == 0) {
printf("Title: %s\n", books[i].title);
found = 1;
}
}
if (!found) {
printf("No books found by author %s.\n", author);
}
}

void find_book_by_title(struct library books[], int count) {


char title[50];
printf("Enter book title: ");
scanf(" %[^\n]s",title);

int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(books[i].title, title) == 0) {
printf("Accession Number: %d\n", books[i].accession_number);
printf("Author: %s\n", books[i].author);
printf("Price: %.2f\n", books[i].price);
printf("Issued: %s\n", books[i].is_issued ? "Yes" : "No");
found = 1;
}
}

if (!found) {
printf("No book found with title %s.\n", title);
}
}

void count_books(int count) {


printf("Total number of books in the library: %d\n", count);
}

void list_books_by_accession_number(struct library books[], int count) {


for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (books[j].accession_number > books[j + 1].accession_number) {
struct library temp = books[j];
books[j] = books[j + 1];
books[j + 1] = temp;
}
}
}
printf("Books in order of accession number:\n");
for (int i = 0; i < count; i++) {
printf("Accession Number: %d, Title: %s\n",
books[i].accession_number, books[i].title);
}
}

int main() {
struct library books[MAX_BOOKS];
int count = 0;
int choice;

do {
printf("\nLibrary Menu:\n");
printf("1. Add book information\n");
printf("2. Display book information\n");
printf("3. List all books of a given author\n");
printf("4. List the title of a specified book\n");
printf("5. List the count of books in the library\n");
printf("6. List the books in the order of accession number\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
add_book(books, &count);
break;
case 2:
display_books(books, count);
break;
case 3:
list_books_by_author(books, count);
break;
case 4:
find_book_by_title(books, count);
break;
case 5:
count_books(count);
break;
case 6:
list_books_by_accession_number(books, count);
break;
case 7:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 7);

return 0;
}

Output.

You might also like