0% found this document useful (0 votes)
33 views55 pages

KPIT Yet To Onboard Assignment Week-3

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)
33 views55 pages

KPIT Yet To Onboard Assignment Week-3

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/ 55

KPIT Yet to Onboard Assignment

Name - Nisheeth Kumar Tripathi


Registration Number – RA2011026010075
SRM Mail Id – [email protected]
Personal Mail Id – [email protected]
UG Course - B.Tech –Computer Science and Engineering with
specialization in Artificial Intelligence and Machine Learning
Phone Number – 9918065751
College – SRM University (KTR)
Chapter-15: Strings

Q1

CODE
#include<stdio.h>
int main()
{
char str1[20];
char str2[20];
char str3[20];
char str4[20];

printf("Enter the string : \n");

scanf("%s%s%s%s",&str1,str2,str3,str4);

printf("Str1 = %s\n",str1);
printf("Str2 = %s\n",str2);
printf("Str3 = %s\n",str3);
printf("Str4 = %s\n",str4);
return 0;
}

OUTPUT
Enter the string :
ALICE GIVE HIM SOMETHING
Str1 = ALICE
Str2 = GIVE
Str3 = HIM
Str4 = SOMETHING
Q2

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

int main() {
char isbn[11];
int sum = 0;

printf("Enter the 10-digit ISBN: ");


scanf("%10s", isbn);

if (strlen(isbn) != 10) {
printf("Invalid ISBN length. Please enter exactly 10 digits.\
n");
return 1;
}

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


int digit;

if (i == 9 && isbn[i] == 'X') {


digit = 10;
} else if (isbn[i] >= '0' && isbn[i] <= '9') {
digit = isbn[i] - '0';
} else {
printf("Invalid character in ISBN. Only digits and 'X' are
allowed.\n");
return 1;
}

sum += digit * (10 - i);


}
if (sum % 11 == 0) {
printf("The ISBN number is correct.\n");
} else {
printf("The ISBN number is incorrect.\n");
}

return 0;
}

OUTPUT
Enter the 10-digit ISBN: 1234567890
The ISBN number is incorrect.

Q3
CODE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int isValidCreditCard(const char *ccNumber) {


int length = strlen(ccNumber);
if (length != 16) {
printf("Credit Card number must be 16 digits long.\n");
return 0;
}

int sum = 0;
for (int i = 0; i < 16; i++) {
int digit = ccNumber[i] - '0';

if ((i % 2) == 0) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}

sum += digit;
}

return (sum % 10 == 0);


}

int main() {
char ccNumber[17];

printf("Enter the 16-digit credit card number: ");


scanf("%16s", ccNumber);

if (isValidCreditCard(ccNumber)) {
printf("The credit card number is valid.\n");
} else {
printf("The credit card number is invalid.\n");
}

return 0;
}

OUTPUT
Enter the 16-digit credit card number: 1234567890123456
The credit card number is invalid.

Chapter-16: Handling Multiple Strings


Q1

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

int isVowel(char ch) {


ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch ==
'u');
}

int main() {
char sentence[81];
char result[81];
int j = 0;

printf("Enter a sentence (max 80 characters): ");


fgets(sentence, 81, stdin);

for (int i = 0; i < strlen(sentence); i++) {


if (!isVowel(sentence[i])) {
result[j++] = sentence[i];
}
}
result[j] = '\0';

printf("Sentence after removing vowels: %s\n", result);

return 0;
}

OUTPUT
Enter a sentence (max 80 characters): HELLO MY NAME IS
NISHEETH
Sentence after removing vowels: HLL MY NMS NSHTH
Q2

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

int isTheWord(char *word) {


if (strcasecmp(word, "the") == 0) {
return 1;
}
return 0;
}

int main() {
char sentence[1001];
char result[1001];
int i = 0, j = 0, k = 0;

printf("Enter a sentence (max 1000 characters): ");


fgets(sentence, sizeof(sentence), stdin);
sentence[strcspn(sentence, "\n")] = '\0';

while (sentence[i] != '\0') {

char word[101];
k = 0;

while (isspace(sentence[i])) {
result[j++] = sentence[i++];
}

// Extract the word


while (!isspace(sentence[i]) && sentence[i] != '\0') {
word[k++] = sentence[i++];
}
word[k] = '\0';

if (!isTheWord(word)) {
k = 0;
while (word[k] != '\0') {
result[j++] = word[k++];
}
}
}
result[j] = '\0';

printf("Sentence after removing 'the': %s\n", result);

return 0;
}

OUTPUT
Enter a sentence (max 1000 characters): JUPITER IS THE LARGST
PLANET
Sentence after removing 'the': JUPITER IS LARGST PLANET

Q3

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

int main()
{
char str[100];
char word[100] = "";
char result[100] = "";
printf("Enter the string : \n");
fgets(str,80,stdin);
int j=0,a=0;
int length = sizeof(str);

for(int i =0; i<length; i++)


{
char ch = str[i];

if(ch!=' ')
{
word[j] = ch;
j++;
}
else
{
int len = sizeof(word);
char x = word[0];
x=toupper(x);
result[a++]=x;
result[a++]='.';
j=0;

}
}

strcat(result,word);

printf("The string is :%s" , result);

return 0;
}

OUTPUT
Enter the string :
Nisheeth Kumar Tripathi
The string is :N.K.Tripathi

Q4

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

int isVowel(char ch) {


ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch ==
'u');
}

int main()
{
char str[100];
fgets(str,80,stdin);
printf("Enter the string : \n");
int j=0;
int length = sizeof(str);

for(int i =0; i<length; i++)

if(isVowel(str[i])&& isVowel(str[i+1]))
{

j++;
}
}
printf("The number of occureences of successive vowels is %d",j);
return 0;
}

OUTPUT
Enter the string :
Read my application thoroughly
The number of occurrences of successive vowels is 3

Q5

CODE
#include <stdio.h>
#include <string.h>
void convertToWords(int num, char *result);
void numberToWords(int num, char *result);
void appendAnd(char *result);

const char *single_digits[] = {"", "One", "Two", "Three", "Four",


"Five", "Six", "Seven", "Eight", "Nine"};
const char *two_digits[] = {"Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
const char *tens_multiple[] = {"", "", "Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
const char *place_values[] = {"Hundred", "Thousand", "Million",
"Billion"};

int main() {
int num;
char result[500] = "";

printf("Enter an integer (up to nine digits): ");


scanf("%d", &num);

if (num == 0) {
printf("Zero\n");
return 0;
}

convertToWords(num, result);
printf("%s\n", result);

return 0;
}

void convertToWords(int num, char *result) {


if (num / 1000000000 > 0) {
numberToWords(num / 1000000000, result);
strcat(result, " ");
strcat(result, place_values[3]);
strcat(result, " ");
num %= 1000000000;
}

if (num / 1000000 > 0) {


numberToWords(num / 1000000, result);
strcat(result, " ");
strcat(result, place_values[2]);
strcat(result, " ");
num %= 1000000;
}

if (num / 1000 > 0) {


numberToWords(num / 1000, result);
strcat(result, " ");
strcat(result, place_values[1]);
strcat(result, " ");
num %= 1000;
}

if (num / 100 > 0) {


numberToWords(num / 100, result);
strcat(result, " ");
strcat(result, place_values[0]);
strcat(result, " ");
num %= 100;
}

if (num > 0) {
if (strlen(result) != 0) {
strcat(result, "and ");
}
numberToWords(num, result);
}
}

void numberToWords(int num, char *result) {


if (num > 19) {
strcat(result, tens_multiple[num / 10]);
if (num % 10 != 0) {
strcat(result, " ");
strcat(result, single_digits[num % 10]);
}
} else if (num >= 10) {
strcat(result, two_digits[num % 10]);
} else if (num > 0) {
strcat(result, single_digits[num]);
}
}

OUTPUT
Enter an integer (up to nine digits): 326384
Six Thousand Three Hundred and Eighty Four
Chapter-17: Structures

Q1

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

// Define the student structure


struct student {
int roll_number;
char name[50];
char department[30];
char course[30];
int year_of_joining;
};

void printStudentsByYear(struct student students[], int count, int


year) {
for (int i = 0; i < count; i++) {
if (students[i].year_of_joining == year) {
printf("%s\n", students[i].name);
}
}
}

void printStudentByRollNumber(struct student students[], int count, int


roll_number) {
for (int i = 0; i < count; i++) {
if (students[i].roll_number == roll_number) {
printf("Roll Number: %d\n", students[i].roll_number);
printf("Name: %s\n", students[i].name);
printf("Department: %s\n", students[i].department);
printf("Course: %s\n", students[i].course);
printf("Year of Joining: %d\n",
students[i].year_of_joining);
return;
}
}
printf("Student with roll number %d not found.\n", roll_number);
}

int main() {

struct student students[450];


int count = 0;

// Sample data for testing


students[0].roll_number = 1;
strcpy(students[0].name, "Alice");
strcpy(students[0].department, "Computer Science");
strcpy(students[0].course, "B.Tech");
students[0].year_of_joining = 2021;

students[1].roll_number = 2;
strcpy(students[1].name, "Bob");
strcpy(students[1].department, "Mechanical Engineering");
strcpy(students[1].course, "B.Tech");
students[1].year_of_joining = 2020;

students[2].roll_number = 3;
strcpy(students[2].name, "Charlie");
strcpy(students[2].department, "Electrical Engineering");
strcpy(students[2].course, "B.Tech");
students[2].year_of_joining = 2021;

count = 3; // Number of students entered

// Test functions
int year_to_search = 2021;
printf("Students who joined in %d:\n", year_to_search);
printStudentsByYear(students, count, year_to_search);

int roll_number_to_search = 2;
printf("\nDetails of student with roll number %d:\n",
roll_number_to_search);
printStudentByRollNumber(students, count, roll_number_to_search);

return 0;
}

OUTPUT
Students who joined in 2021:
Alice
Charlie

Details of student with roll number 2:


Roll Number: 2
Name: Bob
Department: Mechanical Engineering
Course: B.Tech
Year of Joining: 2020

Q2

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

struct bank_account
{
int acc_number;
char name[50];
double balance;
};

void low_balance(struct bank_account customer[], int count)


{
for(int i=0; i<count; i++)
{
if(customer[i].balance<=1000)
{
printf("\nCustomer Name -> %s",customer[i].name);
printf("\nAccount Number -> %d",customer[i].acc_number);
printf("\
n<---------------------------------------------------------------->");
}

}
}

void processTransaction(struct bank_account customer[], int count, int


trans, double amount, int code) {

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


if (customer[i].acc_number == trans) {
if (code == 1) {
customer[i].balance += amount;
printf("Deposit successful. New balance: Rs. %.2lf\n",
customer[i].balance);
} else if (code == 2) {
if (customer[i].balance - amount < 1000.0) {
printf("The balance is insufficient for the
specified withdrawal.\n");
} else {
customer[i].balance -= amount;
printf("Withdrawal successful. New balance: Rs.
%.2lf\n", customer[i].balance);
}
}

return;
}
}
printf("Account number %d not found.\n", trans);
}

int main()
{
struct bank_account customer[200];
int num;
printf("Enter the number of customer : ");
scanf("%d",&num);
for(int i = 0; i<num; i++)
{
printf("\nEnter the details of the customer number : %d ",
i+1 );

printf("\nEnter the account number : ");


scanf("%d",&customer[i].acc_number);

printf("\nEnter the Name of the customer : ");


scanf("%s",&customer[i].name);

printf("\nEnter the balance of the customer : ");


scanf("%lf",&customer[i].balance);
}
int code;

printf("\nDo you want to perform a transaction \n Press 1 for YES


and 0 for NO : ");
scanf("%d",&code);
int trans_acc_number;

if(code == 1)
{
int choice;
printf("\nEnter the Account number : ");
scanf("%d",&trans_acc_number);
printf("\nPress 1 for WITHDRAWL \nPress 2 for DEPOSIT \nEnter
the number : ");
scanf("%d",&choice);
double amount;
printf("\nEnter the amount : ");
scanf("%lf",&amount);

processTransaction(customer,num,trans_acc_number,amount,choice);
}

printf("\nThe Customer having low balance are mentoined below ");


low_balance(customer,num);

return 0;
}

OUTPUT
Enter the number of customer : 3

Enter the details of the customer number : 1


Enter the account number : 1234

Enter the Name of the customer : NISHEETH

Enter the balance of the customer : 25372.863

Enter the details of the customer number : 2


Enter the account number : 5363

Enter the Name of the customer : SHIKHAR

Enter the balance of the customer : 123.3737

Enter the details of the customer number : 3


Enter the account number : 9328

Enter the Name of the customer : VIHSU

Enter the balance of the customer : 678.45

Do you want to perform a transaction


Press 1 for YES and 0 for NO : 1

Enter the Account number : 9328

Press 1 for WITHDRAWL


Press 2 for DEPOSIT
The balance is insufficient for the specified withdrawal.

The Customer having low balance are mentoined below


Customer Name -> SHIKHAR
Account Number -> 5363
<---------------------------------------------------------------->
Customer Name -> VIHSU
Account Number -> 9328
<---------------------------------------------------------------->

Q3

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

struct part {
char serial_number[4];
int year_of_manufacture;
char material[30];
int quantity_manufactured;
};

void retrieveParts(struct part parts[], int count);

int main() {
struct part parts[100];
int count = 0;
int choice;

while (1) {
printf("1. Add Part\n");
printf("2. Retrieve Parts with Serial Numbers between BB1 and
CC6\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

if (choice == 3) break;

switch (choice) {
case 1:
if (count < 100) {
printf("Enter serial number (e.g., AA0): ");
scanf("%s", parts[count].serial_number);
printf("Enter year of manufacture: ");
scanf("%d", &parts[count].year_of_manufacture);
printf("Enter material: ");
scanf("%s", parts[count].material);
printf("Enter quantity manufactured: ");
scanf("%d", &parts[count].quantity_manufactured);
count++;
} else {
printf("Maximum number of parts reached.\n");
}
break;

case 2:
retrieveParts(parts, count);
break;

default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

int isInRange(char serial[]) {


if (strcmp(serial, "BB1") >= 0 && strcmp(serial, "CC6") <= 0) {
return 1;
}
return 0;
}

void retrieveParts(struct part parts[], int count) {


printf("Parts with Serial Numbers between BB1 and CC6:\n");
for (int i = 0; i < count; i++) {
if (isInRange(parts[i].serial_number)) {
printf("Serial Number: %s, Year of Manufacture: %d,
Material: %s, Quantity Manufactured: %d\n",
parts[i].serial_number,
parts[i].year_of_manufacture, parts[i].material,
parts[i].quantity_manufactured);
}
}
}

OUTPUT
1. Add Part
2. Retrieve Parts with Serial Numbers between BB1 and CC6
3. Exit
Enter your choice: 1
Enter serial number (e.g., AA0): BB2
Enter year of manufacture: 2021
Enter material: STEEL
Enter quantity manufactured: 456
1. Add Part
2. Retrieve Parts with Serial Numbers between BB1 and CC6
3. Exit
Enter your choice: 2
Parts with Serial Numbers between BB1 and CC6:
Serial Number: BB2, Year of Manufacture: 2021, Material: STEEL,
Quantity Manufactured: 456
1. Add Part
2. Retrieve Parts with Serial Numbers between BB1 and CC6
3. Exit
Enter your choice: 3
Q4

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

struct cricketer {
char name[50];
int age;
int test_matches;
float average_runs;
};

int compareByAverageRuns(const void *a, const void *b) {


struct cricketer *cricketerA = (struct cricketer *)a;
struct cricketer *cricketerB = (struct cricketer *)b;
if (cricketerA->average_runs < cricketerB->average_runs)
return -1;
else if (cricketerA->average_runs > cricketerB->average_runs)
return 1;
else
return 0;
}

int main() {
int num;
printf("Enter the number of cricketer : \n");
scanf(" %d",&num);
struct cricketer cricketers[num];
int i;

for (i = 0; i < num; i++) {


printf("Enter details for cricketer %d\n", i + 1);
printf("Name: ");
scanf("%s", cricketers[i].name);
printf("Age: ");
scanf("%d", &cricketers[i].age);
printf("Number of test matches: ");
scanf("%d", &cricketers[i].test_matches);
printf("Average runs: ");
scanf("%f", &cricketers[i].average_runs);
}

qsort(cricketers, num, sizeof(struct cricketer),


compareByAverageRuns);

printf("\nCricketers sorted by average runs:\n");


for (i = 0; i < num; i++) {
printf("Name: %s, Age: %d, Test Matches: %d, Average Runs:
%.2f\n",
cricketers[i].name, cricketers[i].age,
cricketers[i].test_matches, cricketers[i].average_runs);
}

return 0;
}

OUTPUT
Enter the number of cricketer :
3
Enter details for cricketer 1
Name: NISHEETH
Age: 22
Number of test matches: 78
Average runs: 65.78
Enter details for cricketer 2
Name: SHIKHAR
Age: 23
Number of test matches: 67
Average runs: 45.90
Enter details for cricketer 3
Name: VISHU
Age: 23
Number of test matches: 77
Average runs: 66.78

Cricketers sorted by average runs:


Name: SHIKHAR, Age: 23, Test Matches: 67, Average Runs: 45.90
Name: NISHEETH, Age: 22, Test Matches: 78, Average Runs: 65.78
Name: VISHU, Age: 23, Test Matches: 77, Average Runs: 66.78

Q5

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

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

struct employee {
int code;
char name[50];
struct date joining_date;
};

int calculateTenure(struct date joining_date, struct date current_date)


{
int years = current_date.year - joining_date.year;
if (current_date.month < joining_date.month ||
(current_date.month == joining_date.month && current_date.day <
joining_date.day)) {
years--;
}
return years;
}

int main() {
int n, i;
struct date current_date;

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


scanf("%d", &n);

struct employee employees[n];

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


printf("Enter details for employee %d\n", i + 1);
printf("Code: ");
scanf("%d", &employees[i].code);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Joining Date (dd mm yyyy): ");
scanf("%d %d %d", &employees[i].joining_date.day,
&employees[i].joining_date.month, &employees[i].joining_date.year);
}

printf("Enter the current date (dd mm yyyy): ");


scanf("%d %d %d", &current_date.day, &current_date.month,
&current_date.year);

printf("Employees with tenure >= 3 years:\n");


for (i = 0; i < n; i++) {
if (calculateTenure(employees[i].joining_date, current_date) >=
3) {
printf("Name: %s\n", employees[i].name);
}
}

return 0;
}
OUTPUT
Enter the number of employees: 3
Enter details for employee 1
Code: 567
Name: NISHETH
Joining Date (dd mm yyyy): 12 12 2012
Enter details for employee 2
Code: 728
Name: SHIKHAR
Joining Date (dd mm yyyy): 23 12 2022
Enter details for employee 3
Code: 672
Name: VISHU
Joining Date (dd mm yyyy): 23 10 2010
Enter the current date (dd mm yyyy): 05 06 2024
Employees with tenure >= 3 years:
Name: NISHETH
Name: VISHU

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

#define MAX_BOOKS 100

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

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


if (*count < MAX_BOOKS) {
printf("Enter accession number: ");
scanf("%d", &books[*count].accession_number);
printf("Enter title: ");
scanf(" %[^\n]s", books[*count].title);
printf("Enter author: ");
scanf(" %[^\n]s", books[*count].author);
printf("Enter price: ");
scanf("%f", &books[*count].price);
books[*count].is_issued = 0;
(*count)++;
} else {
printf("Library is full.\n");
}
}

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


for (int i = 0; i < count; i++) {
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\n", books[i].is_issued ? "Yes" : "No");
}
}

void listBooksByAuthor(struct library books[], int count, char


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

void listTitleByAccessionNumber(struct library books[], int count, int


accession_number) {
for (int i = 0; i < count; i++) {
if (books[i].accession_number == accession_number) {
printf("Title: %s\n", books[i].title);
return;
}
}
printf("Book not found.\n");
}

int countBooks(struct library books[], int count) {


return count;
}

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


struct library temp;
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (books[i].accession_number > books[j].accession_number)
{
temp = books[i];
books[i] = books[j];
books[j] = temp;
}
}
}

displayBooks(books, count);
}

int main() {
struct library books[MAX_BOOKS];
int count = 0;
int choice;
while (1) {
printf("\nLibrary Menu:\n");
printf("1. Add book information\n");
printf("2. Display book information\n");
printf("3. List all books of given author\n");
printf("4. List the title of book specified by accession
number\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:
addBook(books, &count);
break;
case 2:
displayBooks(books, count);
break;
case 3: {
char author[100];
printf("Enter author name: ");
scanf(" %[^\n]s", author);
listBooksByAuthor(books, count, author);
break;
}
case 4: {
int accession_number;
printf("Enter accession number: ");
scanf("%d", &accession_number);
listTitleByAccessionNumber(books, count,
accession_number);
break;
}
case 5:
printf("Total number of books: %d\n", countBooks(books,
count));
break;
case 6:
listBooksByAccessionOrder(books, count);
break;
case 7:
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT
Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 1
Enter accession number: 87394
Enter title: HOROR STOTY OF MAYA
Enter author: NISHEETH
Enter price: 3455.4534

Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 1
Enter accession number: 79303
Enter title: TWISTY TALES
Enter author: ALICE
Enter price: 4239.4332

Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 2
Accession Number: 87394
Title: HOROR STOTY OF MAYA
Author: NISHEETH
Price: 3455.45
Issued: No

Accession Number: 79303


Title: TWISTY TALES
Author: ALICE
Price: 4239.43
Issued: No
Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 3
Enter author name: 4

Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 3
Enter author name: NISHEETH
Title: HOROR STOTY OF MAYA

Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 4
Enter accession number: 79303
Title: TWISTY TALES

Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 5
Total number of books: 2

Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 6
Accession Number: 79303
Title: TWISTY TALES
Author: ALICE
Price: 4239.43
Issued: No

Accession Number: 87394


Title: HOROR STOTY OF MAYA
Author: NISHEETH
Price: 3455.45
Issued: No

Library Menu:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of book specified by accession number
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Enter your choice: 7

Q7

CODE
#include <stdio.h>

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

int compareDates(struct date d1, struct date d2) {


if (d1.day == d2.day && d1.month == d2.month && d1.year == d2.year)
{
return 0;
} else {
return 1;
}
}

int main() {
struct date date1, date2;

printf("Enter first date (dd mm yyyy): ");


scanf("%d %d %d", &date1.day, &date1.month, &date1.year);

printf("Enter second date (dd mm yyyy): ");


scanf("%d %d %d", &date2.day, &date2.month, &date2.year);

if (compareDates(date1, date2) == 0) {
printf("The dates are equal.\n");
} else {
printf("The dates are not equal.\n");
}

return 0;
}
OUTPUT
Enter first date (dd mm yyyy): 10 10 2010
Enter second date (dd mm yyyy): 10 10 2010
The dates are equal.

Chapter-19: Files Input and Output

Q1
CODE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100


#define NAME_LENGTH 50

struct student {
char name[NAME_LENGTH];
int age;
};

int compareNames(const void *a, const void *b) {


struct student *studentA = (struct student *)a;
struct student *studentB = (struct student *)b;
return strcmp(studentA->name, studentB->name);
}

int main() {
FILE *file;
struct student students[MAX_STUDENTS];
int count = 0;

file = fopen("file1.txt", "r");


if (file == NULL) {
printf("Could not open file.\n");
return 1;
}

while (fscanf(file, "%s %d", students[count].name,


&students[count].age) == 2) {
count++;
if (count >= MAX_STUDENTS) {
break;
}
}

fclose(file);

qsort(students, count, sizeof(struct student), compareNames);

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


printf("Name: %s, Age: %d\n", students[i].name,
students[i].age);
}

return 0;
}

OUTPUT
Name: Nisheeth, Age: 20
Name: Shikhar, Age: 22
Name: Vishwajeet, Age: 19

Q2
CODE
#include <stdio.h>
#include <ctype.h>

int main() {
FILE *sourceFile, *targetFile;
char ch;

sourceFile = fopen("source2.txt", "r");


if (sourceFile == NULL) {
printf("Error opening source file.\n");
return 1;
}

targetFile = fopen("target2.txt", "w");


if (targetFile == NULL) {
printf("Error opening target file.\n");
fclose(sourceFile);
return 1;
}

while ((ch = fgetc(sourceFile)) != EOF) {


if (islower(ch)) {
ch = toupper(ch);
}
fputc(ch, targetFile);
}
fclose(sourceFile);
fclose(targetFile);

printf("Contents copied and converted successfully!\n");

return 0;
}

OUTPUT

Contents copied and converted successfully!

Q3
CODE
#include <stdio.h>

int main() {
FILE *file1, *file2, *outputFile;
char line1[100], line2[100];

file1 = fopen("file3_1.txt", "r");


file2 = fopen("file3_2.txt", "r");
if (file1 == NULL || file2 == NULL) {
printf("Error opening input files.\n");
return 1;
}

outputFile = fopen("merged3.txt", "w");


if (outputFile == NULL) {
printf("Error opening output file.\n");
fclose(file1);
fclose(file2);
return 1;
}
while (1) {
if (fgets(line1, sizeof(line1), file1) != NULL) {
fputs(line1, outputFile);
} else {
break;
}

if (fgets(line2, sizeof(line2), file2) != NULL) {


fputs(line2, outputFile);
} else {
break;
}
}

while (fgets(line1, sizeof(line1), file1) != NULL) {


fputs(line1, outputFile);
}
while (fgets(line2, sizeof(line2), file2) != NULL) {
fputs(line2, outputFile);
}

fclose(file1);
fclose(file2);
fclose(outputFile);

printf("Merged lines from both files successfully!\n");

return 0;
}

OUTPUT
Merged lines from both files successfully!

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

struct date {
int d, m, y;
};

struct employee {
int empcode[6];
char empname[20];
struct date join_date;
float salary;
};

void swap(struct employee *a, struct employee *b) {


struct employee temp = *a;
*a = *b;
*b = temp;
}

int compare_dates(struct date d1, struct date d2) {


if (d1.y != d2.y) return d1.y - d2.y;
if (d1.m != d2.m) return d1.m - d2.m;
return d1.d - d2.d;
}

void sort_by_join_date(struct employee arr[], int n) {


for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (compare_dates(arr[j].join_date, arr[j + 1].join_date) >
0) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}

int main() {
FILE *inputFile, *outputFile;
struct employee records[10];
int numRecords;

inputFile = fopen("employee_records6.txt", "r");


if (inputFile == NULL) {
printf("Error opening input file.\n");
return 1;
}

numRecords = 0;
while (fscanf(inputFile, "%d %s %d %d %d %f",
&records[numRecords].empcode[0],
records[numRecords].empname,
&records[numRecords].join_date.d,
&records[numRecords].join_date.m,
&records[numRecords].join_date.y,
&records[numRecords].salary) == 6) {
++numRecords;
}

fclose(inputFile);

sort_by_join_date(records, numRecords);

outputFile = fopen("sorted_employee_records.txt", "w");


if (outputFile == NULL) {
printf("Error opening output file.\n");
return 1;
}

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


fprintf(outputFile, "%d %s %d %d %d %.2f\n",
records[i].empcode[0],
records[i].empname,
records[i].join_date.d,
records[i].join_date.m,
records[i].join_date.y,
records[i].salary);
}

fclose(outputFile);

printf("Employee records sorted and written to


sorted_employee_records.txt.\n");

return 0;
}

OUTPUT
Employee records sorted and written to sorted_employee_records.txt.

Q7

CODE
#include <stdio.h>

struct BloodDonor {
char name[20];
char address[40];
int age;
int bloodType;
};

int main() {
FILE *file;
struct BloodDonor donor;

file = fopen("blood_donors7.txt", "r");


if (file == NULL) {
printf("Error opening file.\n");
return 1;
}

printf("Blood donors with age below 25 and blood type 2:\n");

while (fread(&donor, sizeof(struct BloodDonor), 1, file)) {


printf("%d hello\n",donor.bloodType);
if (donor.age < 25 && donor.bloodType == 2) {

printf("Name: %s, Age: %d\n", donor.name, donor.age);


}
}

fclose(file);

return 0;
}

OUTPUT
The output I was getting was incorrect

Q8
CODE
#include <stdio.h>
#include <stdlib.h>

#define MAX_STUDENTS 100

int main() {
char studentNames[MAX_STUDENTS][50];
int numStudents, n;

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


scanf("%d", &numStudents);

if (numStudents <= 0 || numStudents > MAX_STUDENTS) {


printf("Invalid number of students. Please enter a value
between 1 and %d.\n", MAX_STUDENTS);
return 1;
}

printf("Enter the names of %d students:\n", numStudents);


for (int i = 0; i < numStudents; ++i) {
scanf("%s", studentNames[i]);
}

FILE *file = fopen("student_names8.txt", "w");


if (file == NULL) {
printf("Error opening file for writing.\n");
return 1;
}

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


fprintf(file, "%s\n", studentNames[i]);
}

fclose(file);
printf("Student names saved to student_names.txt.\n");
printf("Enter the value of n (1 to %d): ", numStudents);
scanf("%d", &n);

if (n < 1 || n > numStudents) {


printf("Invalid value of n. Please enter a value between 1 and
%d.\n", numStudents);
return 1;
}

printf("The %dth student name is: %s\n", n, studentNames[n - 1]);

return 0;
}

OUTPUT
Enter the number of students: 4
Enter the names of 4 students:
Nisheeth
Shikhar
Vishu
Sani
Student names saved to student_names.txt.
Enter the value of n (1 to 4): 4
The 4th student name is: Sani
Q9
CODE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Student {
int rollNumber;
char name[50];
};

int main() {
FILE *masterFile, *transactionFile, *updatedFile;
struct Student students[100];
int numStudents = 0;
char operation;
int rollNumber;
char name[50];

masterFile = fopen("master_students9.txt", "r");


if (masterFile == NULL) {
printf("Error opening Master file.\n");
return 1;
}

while (fscanf(masterFile, "%d %[^\n]",


&students[numStudents].rollNumber, students[numStudents].name) == 2) {
numStudents++;
}

fclose(masterFile);

transactionFile = fopen("transaction_changes9.txt", "r");


if (transactionFile == NULL) {
printf("Error opening Transaction file.\n");
return 1;
}

while (fscanf(transactionFile, " %c %d %[^\n]", &operation,


&rollNumber, name) == 3) {
if (operation == 'A') {
students[numStudents].rollNumber = rollNumber;
strcpy(students[numStudents].name, name);
numStudents++;
} else if (operation == 'D') {
for (int i = 0; i < numStudents; i++) {
if (students[i].rollNumber == rollNumber) {
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
numStudents--;
break;
}
}
}
}

fclose(transactionFile);

updatedFile = fopen("updated_students9.txt", "w");


if (updatedFile == NULL) {
printf("Error opening Updated file.\n");
return 1;
}

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


fprintf(updatedFile, "%d %s\n", students[i].rollNumber,
students[i].name);
}

fclose(updatedFile);
printf("Updated student records saved to updated_students.txt.\n");

return 0;
}

OUTPUT
Q10
CODE
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void removeArticles(char *word) {

for (int i = 0; word[i]; ++i) {


word[i] = tolower(word[i]);
}

if (strcmp(word, "a") == 0 || strcmp(word, "the") == 0 ||


strcmp(word, "an") == 0) {
strcpy(word, "");
}
}

int main() {
FILE *inputFile, *outputFile;
char word[100];

inputFile = fopen("input_text10.txt", "r");


if (inputFile == NULL) {
printf("Error opening input file.\n");
return 1;
}

outputFile = fopen("output_text10.txt", "w");


if (outputFile == NULL) {
printf("Error opening output file.\n");
fclose(inputFile);
return 1;
}
while (fscanf(inputFile, "%s", word) == 1) {
removeArticles(word);
fprintf(outputFile, "%s ", word);
}

fclose(inputFile);
fclose(outputFile);

printf("Updated content saved to output_text10.txt.\n");

return 0;
}

OUTPUT

Updated content saved to output_text10.txt.


Chapter-21: Operation On Bits

Q1

CODE
#include <stdio.h>

int main() {
unsigned short int num;
printf("Enter an unsigned 16-bit integer: ");
scanf("%hu", &num);

unsigned char byte1 = num & 0xFF;


unsigned char byte2 = (num >> 8) & 0xFF;

unsigned short int swappedNum = (byte1 << 8) | byte2;

printf("Original number: %hu\n", num);


printf("Swapped number: %hu\n", swappedNum);

return 0;
}

OUTPUT
Enter an unsigned 16-bit integer: 564
Original number: 564
Swapped number: 13314
Q2

CODE
#include <stdio.h>

int main() {
unsigned char num;
printf("Enter an 8-bit number: ");
scanf("%hhu", &num);

unsigned char higherBits = num >> 4;


unsigned char lowerBits = num & 0x0F;

unsigned char swappedNum = (lowerBits << 4) | higherBits;

printf("Original number: %u\n", num);


printf("Swapped number: %u\n", swappedNum);

return 0;
}

OUTPUT
Enter an 8-bit number: 65
Original number: 65
Swapped number: 20

Q3

CODE
#include <stdio.h>

int main() {
unsigned char num;
printf("Enter an 8-bit number: ");
scanf("%hhu", &num);

num |= 0xAA;

printf("Modified number: %u\n", num);

return 0;
}
OUTPUT
Enter an 8-bit number: 34
Modified number: 170

Q4

CODE
#include <stdio.h>

int main() {
unsigned char num;
printf("Enter an 8-bit number: ");
scanf("%hhu", &num);

if ((num & (1 << 2)) && (num & (1 << 4))) {

num &= ~(1 << 2);


num &= ~(1 << 4);
printf("Modified number: %u\n", num);
} else {
printf("No action needed. Bits 3 and 5 are not both set.\n");
}

return 0;
}

OUTPUT
Enter an 8-bit number: 533
Modified number: 1

You might also like