c file

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 120

All Assignment File

Lab Assignment 1
Q1. Write a C program that takes the number of subjects and total
marks as input from the user, computes the percentage, and prints it
on the screen.
#include<stdio.h>
int main()
{
//declare variables
int radius;
float area;
printf("Enter the radius:\n");
scanf("%d",&radius);
area=3.14*radius*radius;
printf("Area of the circle is:%f\n",area);
}

Output:

Q2. Write a C program that takes two numbers from the user and
computes their sum, multiplication, division, and remainder, then
prints the output.
#include<stdio.h>
int main()
{
//declare variables
int no_subjects;
float total_marks;
float percentage;
printf("Enter the number of subjects:\n");
scanf("%d",&no_subjects);
printf("Enter Your marks:\n");
scanf("%f",&total_marks);
percentage=total_marks/no_subjects;
printf("Your percentage is:%f",percentage);
return 0;
}

Output:

Q3. Write a C program that takes the radius as input from the user and
prints the area of the circle.
#include<stdio.h>
int main()
{
//declare variables
int num1;
int num2;
int sum;
int multiplication;
float division;
int remainder;
int quotient;
printf("Enter the first number:\n");
scanf("%d",&num1);
printf("Enter the second number:\n");
scanf("%d",&num2);
sum=num1+num2;
printf("The sum of the numbers %d and %d is: %d \
n",num1,num2,sum);

multiplication=num1*num2;
printf("Multiplication of numbers %d and %d is: %d \
n",num1,num2,multiplication);

division=(float)num1/num2;
printf("Division of numbers %d and %d is: %f\
n",num1,num2,division);

remainder=num1%num2;
quotient=num1/num2;
printf("When %d is divided by %d, the quotient is %d and the
remainder is: %d\n",num1,num2,quotient,remainder);
return 0;
}

Lab Assignment 2
Exercise 3.1 A c program contains the following statements: #include
int i, j; long ix; short s; unsigned u; float x; double dx; char c; For
each of the following groups of variables, write a scanf function that
will allow a set of data items to be read into the computer and
assigned to the variables. Then write a printf function that will allow
the values of the variables to be displayed. (a) i ,j, x and dx (b) i, ix, j,
x, and u (c) i, u and c (d) c, x, dx and s
Exercise 3.2. Size of different datatypes in byte Run the following
program and observe the output.
#include<stdio.h>
int main()
{
int i,j;
long ix;
short s;
unsigned u;
float x;
double dx;
char c;

printf("(a):Enter the values for i,j,x,dx:\n");


scanf("%d,%d,%f,%lf",&i,&j,&x,&dx);
printf("The values are i=%d,j=%d,x=%f,dx=%lf\n",i,j,x,dx);
printf("\n")
printf("(b):Enter the values for i,j,ix,x,u:\n");
scanf("%d,%d,%ld,%f,%u",&i,&j,&ix,&x,&u);
printf("The values are i=%d,j=%d,ix=%ld,x=%f,u=%u\
n",i,j,ix,x,u);
printf("\n");
printf("(c):Enter the values for i,u,c:\n");
scanf("%d,%u,%c",&i,&u,&c);
printf("The values are i=%d,u=%u,c=%c\n",i,u,c);
printf("\n");
printf("(d):Enter the values for x,c,dx,s:\n");
scanf("%f,%c,%lf,%hd",&x,&c,&dx,&s);
printf("The values are x=%f,c=%c,dx=%lf,s=%hd \n",x,c,dx,s);
return 0;
}

Output:
Lab Assignment 3
1.(a) Any character is entered through the keyboard, write a program
to determine whether the character entered is a capital letter, a small
case letter, a digit or a special symbol. The following table shows the
range of ASCII values for various characters.
(b) Any character is entered through the keyboard, write a program to
convert lowercase characters to uppercase and vice versa. If the
character is not a letter, print "Invalid character."

#include<stdio.h>
int main()
{
char ch;
printf("enter a char:");
scanf("%c",&ch);
if (ch >= 65 && ch <= 90)
{
printf("Entered character is a Upper case!");
}
else if
(ch >= 97 && ch <=122)
{
printf("Entered character is Lower case!");
}
else if
(ch >= 48 && ch <=57)
{
printf("Entered character is digit between 0 and 9!");
}
else if
(ch >= 0 && ch <=47 || ch >= 58 && ch <=64 || ch >= 91 && ch
<=96 || ch >= 124 && ch <=127)
{
printf("Entered character is a special character!");
}
else
{
printf("Ïnvalid character");
}
return 0;
}
b)

#include<stdio.h>
int main()
{
char ch;
printf("enter a char:");
scanf("%c",&ch);
if(ch >= 97 && ch <= 122)
{
printf("%c",(int)ch-32);
}

else if(ch >= 65 && ch <= 90)


{
printf("%c",(int)ch+32);
}
else
{
printf("invalid char");
}
return 0;
}

2. (a) Run the following program and observe the ouput:


(b) Write a program to find the ACII value of the following escape
sequences: /* Hint printf("ASCII value of \\a is: %d", '\a');
#include<stdio.h>
int main()
{
printf("ASCII value of \\a is: %d\n", '\a');
printf("ASCII value of \\b is: %d\n", '\b');
printf("ASCII value of \\t is: %d\n", '\t');
printf("ASCII value of \\v is: %d\n", '\v');
printf("ASCII value of \\n is: %d\n", '\n');
printf("ASCII value of \\f is: %d\n", '\f');
printf("ASCII value of \\r is: %d\n", '\r');
printf("ASCII value of \\\" is: %d\n",'\"');
printf("ASCII value of \\' is: %d\n", '\'');
printf("ASCII value of \\? is: %d\n", '\?');
printf("ASCII value of \\\\ is: %d\n", '\\');
printf("ASCII value of \\0 is: %d\n", '\0');

return 0; }

3. Write a program to swap two numbers using the follwowing


methods. (a) Using a Temporary Variable (b) Using Arithmetic
Operations (c) Using Bitwise XOR Operation.
a)
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter two numbers to be swapped:");
scanf("%d %d",&a,&b);
c=a;
a=b;
b=c;
printf("swapped numbers are:%d %d",a,b);
return 0; }

b)
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the values to be swapped:");
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("The swapped values are using arithemetic operations: %d
%d",a,b);
}
c)
int main()
{
int a,b;
printf("Enter the values to be swapped:");
scanf("%d %d",&a,&b);
a=a^b;
b=a^b;
a=a^b;
printf("The swapped values are using bitwise xor operations: %d
%d",a,b);
}
Lab Assignment 4
Q1.[if-else] Write a program that prints x cubed if the input x is even;
otherwise, it prints x after squaring. The data type of x is integer. (a)
Use of the modulus (%) operator is allowed. (b) Use of the modulus
(%) operator is not allowed. /* Hint: rem = x - quotient * 2 */ (c) Use
the bitwise AND operator (&) to check if the number is even or odd.
a)
#include<stdio.h>
int main()
{
int x;
printf("Enter a number:");
scanf("%d",&x);
if(x%2==0)
{
printf("%d",x*x*x);
}
else
{
printf("%d",x*x);
}
return 0;
}

b)
{
int num,quotient,remainder;
printf("Enter a number:");
scanf("%d",&num);
quotient = num / 2;
remainder = num - quotient * 2;
if (remainder == 0)
{
printf("The value is: %d\n", num * num * num);
}
else
{
printf("The value is: %d\n", num * num);
}
return 0;
}

c)
#include <stdio.h>

int main(){
int num;
printf("Enter a number: ");
scanf("%d",&num);
if(num&1==1){
printf("Square of number is %d",num*num);
}
else {
printf("cube of number is %d",num*num*num);
}
return 0;
}
Q2. [switch-case] Write a simple calculator program that takes two
numbers and an operator from the user and prints the result of the
operation.
#include <stdio.h>

int main()
{
int num1, num2;
char a;
printf("Enter num1: ");
scanf("%d",&num1);
printf("Enter num2: ");
scanf("%d",&num2);
printf("Enter any operator from +,-,*,/: ");
scanf (" %c",&a); //need to put
space before %c when assigning value to char

switch (a)
{
case '+':
printf("%d",num1+num2);
break;
case '-':
printf("%d",num1-num2);
break;
case '*':
printf("%d",num1*num2);
break;
case '/':
if(num2!=0)
{
printf("%d",num1/num2);
}
else
{
printf("Error: Division by zero is not allowed.");
}
break;
default:
printf("Error: Invalid operator. Please use +, -, *, or /");
}
return 0;
}
Q3 [if else and switch case]: Write a program that takes a character
input from the user and determines whether it is a vowel. (a) Using if-
else (b) Using switch-case
a)
int main(){
char a;
printf("Enter alphabet: ");
scanf("%c",&a);
if(a=='a'||a=='A'||a=='e'||a=='E'||a=='i'||a=='I'||a=='o'||a=='O'||a=='u'||
a=='U'){
printf("The alphabet is a vowel");
}
else {
printf("The aplhabet is a not a vowel");
}
return 0;
}
b)
#include <stdio.h>
int main(){
char a;
printf("Enter an alphabet: ");
scanf("%c",&a);
switch(a){
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("The alphabet is a vowel");
break;
default:
printf("The alphabet is a not a vowel");
}
return 0;
}

Q4: [while loop] Write a program that calculates (a) the sum of the
first n natural numbers, (b) the sum of their squares, and (c) the sum
of their cubes.
#include<stdio.h>
int main()
{
int count,n,sum=0,sqr=0,cube=0;

printf("Enter a positive integer (n): ");


scanf("%d",&n);

count =1;
while(count<=n){
sum = sum + count;
count++;
}

count=1;
while(count<=n){
sqr = sqr + (count*count);
count++;
}

count=1;
while(count<=n){
cube = cube + (count*count*count);
count++;
}

printf("Sum of the first %d natural numbers: %d\n",n,sum);


printf("Sum of the squares of the first %d natural numbers: %d\
n",n,sqr);
printf("Sum of the cubes of the first %d natural numbers: %d\
n",n,cube);

return 0;
}
Q5: [while loop] Write a program that calculates the factorial of a
given positive integer.
#include<stdio.h>
int main()
{
int n,fac=1,count=1;

printf("Enter a positive integer: ");


scanf("%d",&n);

if (n>=0)
{
while (count<=n)
{
fac = fac*count;
count++;
}
printf("factorial of %d = %d",n,fac);
}
else
{
printf("Error! Factorial of a negative number doesn't exist");
}
return 0;
}

Lab Assignment 5
Q1.[while and for] Write a program that takes an integer input from
the user and prints its multiplication table up to 10. (a) Using while
loop (b) Using for loop
a)
#include<stdio.h>
int main()
{
int number,count;
printf("Enter a number:");
scanf("%d",&number);
while(count<=10)
{
printf("%d x %d = %d\n",number,count,number*count);
count++;
}
return 0;
}

b)
#include<stdio.h>
int main()
{
int number;
printf("Enter a number:");
scanf("%d",&number);
for(int count=1;count<=10;count++)
{
printf("%d x % d = %d\n",number,count,number*count);
}
return 0;
}

Q2 [Nested loop-Right Half Pyramid Pattern]


(a)
1
12
123
1234
12345
(b)
A
AB
ABC
ABCD
ABCDE
(c)
A
BC
DEF
GHIJ
KLMNO
a)
#include<stdio.h>
int main()
{
int rows = 5;
for(int i=1;i<=rows;i++)
{
for(int j = 1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
b)
#include<stdio.h>
int main()
{
int rows=69;
for(int i=65;i<=rows;i++)
{
for(int j = 65;j<=i;j++)
{
printf("%c ",j);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int rows = 5;
char c = 'A';

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {


printf("%c ",c);
c++;
}
printf("\n");
}
return 0;
}
Q3: [Full Pyramid Pattern] Write a program to print a full pyramid
pattern using nested loops.
Now, Write a program to print the following patterns
(a)
1
123
12345
1234567
123456789
(b)
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
a)
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 2 * (rows - i) - 1; j++) {
printf(" ");
}

for (int k = 1; k <= 2 * i + 1; k++) {


printf("%d ",k);
}
printf("\n");
}
return 0;
}

b)
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 2 * (rows - i) - 1; j++) {
printf(" ");
}
char c = 'A';
for (int k = 1; k <= 2 * i + 1; k++) {
printf("%c ",c);
c++;
}
printf("\n");
}
return 0;
}

Lab Assignment 6
Q1. [Functions] Sum of Two Numbers using a Function
#include <stdio.h>

int add(int a, int b){


int s = a + b;
return s;
}

int main()
{
int a, b;

printf("Enter two numbers: ");


scanf("%d %d",&a,&b);

printf("Sum = %d",add(a,b));

return 0;
}
Output:

Q2 [Function] GCD of Two Numbers using a Function


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

int gcd(int a, int b){

int x, temp;

if (a>b)
{
temp = a;
a = b;
b = temp;
}
for (int i=1;i<=a;i++){
if (a%i){
continue;
}
if (b%i) {
continue;
}
x = i;
}
return x;
}

int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d",x,y);

printf("GCD = %d",gcd(x,y));

return 0;
}
Output:

Q3[function and pointer]: Swapping Two Numbers using Pointers


#include <stdio.h>

int swap(int *a,int *b)


{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

int main()
{
int x,y;

printf("Enter two numbers: ");


scanf("%d %d",&x,&y);

swap(&x,&y);

printf("x = %d, y = %d",x,y);


}

Q4 [array and function]: Calculate the Average of an Array


#include <stdio.h>
float calculateAverage(int arr[], int n) {
int sum=0;
float average;

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


sum = sum + arr[i];
}
average = (float)sum / n;
printf("Average = %.2f",average);
}

int main() {

int marks[10], n;

printf("Enter number of elements: ");


scanf("%d", &n);

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


printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
}

calculateAverage(marks,n);

return 0;
}
Output:
Q5 [array and function]: Reverse an Array using a Function
#include <stdio.h>
void reverseArray(int arr[], int n)
{
int revarr[5];

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


{
revarr[i] = arr[n - 1 - i];
}
printf("Reversed array: ");
for(int i = 0; i < n; i++)
{
printf("%d ", revarr[i]);
}
}

int main()
{
int arr[5];

printf("Enter 5 numbers: ");


for(int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}

reverseArray(arr, 5);

return 0;
}
Output:

Q6 [array, pointer and function]: Print Address of an Array Element


using Pointers
#include <stdio.h>

void printAddress(int *arr, int n)


{
for(int i=0;i<n;i++)
{
printf("Address of element %d = %d \n",i+1,&arr[i]);
}
}

int main()
{
int arr[3];

printf("Enter 3 numbers: ");


for(int i = 0; i < 3; i++)
{
scanf("%d", &arr[i]);
}
printAddress(arr, 3);
return 0;
}
Output:
Q7 [array, function and pointer]: Display the Elements of an Array
using Pointers
#include <stdio.h>

void displayArray(int *arr, int n)


{

printf("Array elements: ");

for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}

int main()
{
int arr[5];

printf("Enter 5 numbers: ");


for(int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}

displayArray(arr, 5);

return 0;
}
Q8[array, function, pointer]:Compare Two Arrays using a Function
#include<stdio.h>

int compareArrays(int arr1[], int arr2[], int n)


{
for (int i=0;i<n;i++)
{
if (arr1[i]!=arr2[i])
{
return 0;
}
}
return 1;
}

int main()
{
int arr1[5], arr2[5],x;
printf("Enter 5 elements of the first array: ");
for(int i=0; i < 5; i++)
{
scanf("%d", &arr1[i]);
}
printf("Enter 5 elements of the second array: ");
for(int i = 0; i < 5; i++)
{
scanf("%d", &arr2[i]);
}

x = compareArrays(arr1, arr2, 5);

if(x==1)
{
printf("The arrays are identical.");
}
else
{
printf("The arrays are different.");
}

return 0;
}
Outputs:
Lab Assignment 7
Q1. [do-while loop and functions] Write a simple calculator program
in C that performs basic arithmetic operations (addition, subtraction,
multiplication, and division) using a do-while loop. The calculator
should continuously prompt the user for input until they choose to
exit.
#include <stdio.h>
float add(float num1, float num2){
return num1+num2;
}
float subtract(float num1, float num2){
return num1-num2;
}
float multiply(float num1, float num2){
return num1*num2;
}
float divide(float num1, float num2){
return num1/num2;
}
int main(){
float x, y;
char a,choice;
do {
printf("Enter two numbers: ");
scanf("%f %f",&x,&y);
printf("Choose an operator (+, -, *, /): ");
scanf("\n %c",&a);
switch(a) {
case '+':
printf("%.2f %c %.2f = %.2f\n",x,a,y,add(x,y));
break;
case '-':
printf("%.2f %c %.2f = %.2f\n",x,a,y,subtract(x,y));
break;
case '*':
printf("%.2f %c %.2f = %.2f\n",x,a,y,multiply(x,y));
break;
case '/':
if(y!=0){
printf("%.2f %c %.2f = %.2f\n",x,a,y,divide(x,y));
break;
}
printf("Error: Division by zero is not allowed.");
break;
}
printf("\nDo you want to perform another calculation? (Press
'n'to exit): ");
scanf(" %c",&choice);
}
while (choice=='y');
return 0;
}
Output:

Q2. [loop and function] Write a C program that includes two


functions: (a) one to check if a number is prime and (b) another to
return the minimum of two numbers.
#include <stdio.h>
int isPrime(int num){
if(num%2||num%3||num%5||num%7||num%11||num%13||num%17||
num%19||num%23||num
%29||num%31) {
return 1;
}
else{
return 0;
}
}
int min(int a, int b){ if(a>b){
return b;
}
else{
return a;
}
}
int main(){
int x, y;
printf("Enter a number to check if it's prime: "); scanf("%d",&x);
if(isPrime(x)==1){
printf("%d is a prime number.\n",x);
}
else{
printf("%d is not a prime number.\n",x);
}

printf("Enter two numbers to find the minimum: "); scanf("%d


%d",&x,&y);

if(min(x,y)==x){
printf("The minimum of %d and %d is %d\n",x,y,x);
}
else{
printf("The minimum of %d and %d is %d\n",x,y,y);
}
return 0;
}
Output:

Q3 [function and loop] Write a C program that includes a function to


calculate and print the product of the first n natural numbers.
//1X2X3…..n long long int productOfNaturalNumbers(int n); (a)
Without using recursion (b) With recursive function
a)
#include <stdio.h>

double pro(int n){ double ans=1;


for(int i=1;i<=n;i++){ ans = ans*i;
}
return ans;
}
int main(){
int x, y;

printf("Enter a number to calculate the product of the first n natural


numbers: ");
scanf("%d",&x);

printf("The product of the first %d natural numbers is:


%.0lf",x,pro(x));
return 0;
}
Output:

b)
#include <stdio.h>
double pro(int n){ double ans=n;
if(n==1){
return 1;
}
else{
ans*n;
}
return ans*pro(n-1);
}
int main(){
int x, y;

printf("Enter a number to calculate the product of the first n natural


numbers: ");
scanf("%d",&x);
printf("The product of the first %d natural numbers is:
%.0lf",x,pro(x));
return 0;
}
Output:

Q4 [Function and loop] Write a C program that includes a function to


calculate m^n (m raised to the power of n) using a loop. //assume n is
a positive integer double power(double m, int n); (a) Using recursion
(b) Without using recursion
a)
#include <stdio.h>
double power(double m, int n){ double ans=m;
if(n==1){
return m;
}
return ans*power(m,n-1);
}
int main(){
int m,n;

printf("Enter the base number (m): ");


scanf("%d",&m);
printf("Enter the exponent (n): ");
scanf("%d",&n);
printf("The result of %d^%d is: %.0lf",m,n,power(m,n));
return 0;
}
Output:
b)
#include <stdio.h>

double power(double m, int n){ double ans=1;

if(n==0){
return 1;
}
for(int i=1;i<=n;i++){ ans = ans*m;
}
return ans;
}
int main(){
int m,n;
printf("Enter the base number (m): ");
scanf("%d",&m);
printf("Enter the exponent (n): ");
scanf("%d",&n);
printf("The result of %.0d^%.0d is: %.0lf",m,n,power(m,n));
return 0;
}
Output:
Lab Assignment 8
Q1)
Searching an Element in an Array Objective: Write a program to
search for an element in an array using both iterative and recursive
approaches. int search(int arr[], int n, int key); This function should
return:
● -1 if the key is not present in the array.
● 1 if the key is found in the array.
Tasks: 1. Without recursion: Implement the search using a loop. 2.
With recursion: Implement the search using recursion
a)
#include <stdio.h>
void readArray(int arr[], int n) {
printf("Enter %d elements of the array: ",n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
}
void printArray(int arr[], int n) {
printf("Array elements are: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int search(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return 1;
}
}
return -1;
}
int main() {
int n, key;
printf("Enter size of the array: ");
scanf("%d", &n);
int arr[n];
readArray(arr, n);
printArray(arr, n);
printf("Enter the key to search: ");
scanf("%d", &key);
if (search(arr, n, key) == 1)
printf("Element %d is present in the array.\n", key);
else
printf("Element %d is not present in the array.\n", key);
return 0;
}
Output:
b)
#include <stdio.h>
void readArray(int arr[], int n) {
printf("Enter %d elements of the array: ",n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
}
void printArray(int arr[], int n) {
printf("Array elements are: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int search(int arr[], int n, int key) {
if (n==0) {
return -1;
}
if (n==1) {
if(arr[0]==key)
return 1;
else
return -1;
}
if(arr[0]==key) {
return 1;
}
return search(arr+1, n-1, key);
}
int main() {
int n, key;
printf("Enter size of the array: ");
scanf("%d", &n);
int arr[n];
readArray(arr, n);
printArray(arr, n);
printf("Enter the key to search: ");
scanf("%d", &key);
if (search(arr, n, key) == 1)
printf("Element %d is present in the array.\n", key);
else
printf("Element %d is not present in the array.\n", key);
return 0;
}
Output:

Q 2: Search an Element in a String Objective: Solve the search


problem for strings. Read a string with spaces and search for a
character.
a)
#include <stdio.h>

void readString(char* str) {


printf("Enter a string: ");
scanf("%[^\n]s", str);
}

void printString(char* str) {


printf("The string is: %s\n", str);
}

int search(char* str, char key) {


for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == key) {
return 1;
}
}
return -1;
}

int main() {
char key;
char arr[100];

readString(arr);
printString(arr);

printf("Enter the character to search: ");


scanf(" %c", &key);

int result = search(arr, key);


if (result == 1)
printf("Character '%c' found in the string.\n", key);
else
printf("Character '%c' not found in the string.\n", key);

return 0;
}
Output:

b)
#include <stdio.h>

void readString(char* str) {


printf("Enter a string: ");
scanf("%[^\n]s", str);
}

void printString(char* str) {


printf("The string is: %s\n", str);
}

int search(char* str, char key) {


if(str[0]=='\0'){
return -1;
}
if(str[0]==key){
return 1;
}
else {
return search(str+1, key);
}
}

int main() {
char key;
char arr[100];

readString(arr);
printString(arr);

printf("Enter the character to search: ");


scanf(" %c", &key);

int result = search(arr, key);


if (result == 1)
printf("Character '%c' found in the string.\n", key);
else
printf("Character '%c' not found in the string.\n", key);

return 0;
}
Output:

Q3: Reverse an Array Objective: Reverse the array using two


approaches: 1. Using a temporary array. 2. In-place reversal.
#include <stdio.h>
#include <stdlib.h>
void reverse_array(int arr[], int tem[], int n) {
for(int i=0; i<n; i++){
tem[i]=arr[n-1-i];
}
for(int i=0; i<n; i++){
arr[i]=tem[i];
}
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d",&n);

int *arr = (int*)malloc(n*sizeof(int*));


int *tem = (int*)malloc(n*sizeof(int*));

printf("Enter %d elements of the array: ",n);


for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}

reverse_array(arr, tem, n);

printf("Reversed array: ");


for(int i=0;i<n;i++) {
printf("%d ",arr[i]);
}

return 0;
}

Output:

b)
#include <stdio.h>
#include <stdlib.h>

void reverse_array(int arr[], int n) {


int temp;
for (int i = 0; i < n / 2; i++) {
temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d", &n);
int *arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Enter %d elements of the array: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

reverse_array(arr, n);

printf("Reversed array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

free(arr);
return 0;
}
Output:

c)

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

void swap(int* n, int* m) {


int temp = *n;
*n = *m;
*m = temp;
}

void reverse_array(int arr[], int n) {


if (n <= 1) {
return;
}
swap(&arr[0], &arr[n - 1]);
reverse_array(arr + 1, n - 2);
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d", &n);
int *arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Enter %d elements of the array: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

reverse_array(arr, n);

printf("Reversed array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

free(arr);
return 0;
}
Output:

d)
#include <stdio.h>
#include <stdlib.h>

void swap(int* n, int* m) {


int temp = *n;
*n = *m;
*m = temp;
}
void reverse_array(int arr[], int n) {
for(int i=0; i<n/2; i++) {
swap(&arr[i], &arr[n - 1 - i]);
}
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));


if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Enter %d elements of the array: ", n);


for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

reverse_array(arr, n);

printf("Reversed array: ");


for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

free(arr);

return 0;
}
Output:

Q4: Find the Maximum Element in an Array Objective: Write a


program to find the maximum element in an array using both iterative
and recursive approaches.
a)
#include <stdio.h>
#include <stdlib.h>

int find_max(int arr[], int n) {


int a = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > a) {
a = arr[i];
}
}
return a;
}

int main() {
int n;
printf("Enter size of the array: ");
scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));


if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Enter %d elements of the array: ", n);


for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Maximum element in the array is %d", find_max(arr, n));

free(arr);

return 0;
}
Output:

b)
#include <stdio.h>
#include <stdlib.h>

int find_max_rec(int arr[], int n) {


if (n == 1) {
return arr[0];
}
int max_of_rest = find_max_rec(arr, n-1);
return (arr[n-1] > max_of_rest) ? arr[n-1] : max_of_rest;
}

int main() {
int n;
printf("Enter size of the array: ");
scanf("%d", &n);

int* arr = (int*)malloc(n*sizeof(int));

printf("Enter %d elements of the array: ", n);


for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Maximum element in the array is %d\n", find_max_rec(arr,
n));
free(arr);
return 0;
}
Output:

Lab Assignment 9
Q1) Write a C program to find the nth term of the Fibonacci series.
The Fibonacci sequence is defined as follows: ● F(0) = 0 ● F(1) = 1 ●
F(n) = F(n-1) + F(n-2), for n ≥ 2
#include <stdio.h>

int fib(int n) {
if(n==0){
return 0;
}
if(n==1){
return 1;
}
if(n>1){
return fib(n-1)+fib(n-2);
}
}

int main() {
int n;

printf("Enter the term you want to find: ");


scanf("%d",&n);

printf("%dth term of Fibonacci sequence is %d",n,fib(n));

return 0;
}
Output:
Q2) Write a C program to implement the Ackermann function. The
Ackermann function is a well-known recursive mathematical function
defined as follows: A(m,n)={ n+1 if m=0 A(m−1,1) if m>0 and n=0
A(m−1, A(m,n−1)) if m>0 and n>0 } Here, m and n are two non-
negative integers
#include <stdio.h>

int ackermann(int m, int n){


if(m==0){
return n+1;
}
if((m>0)&&(n==0)){
return ackermann(m-1, 1);
}
if((m>0)&&(n>0)){
return ackermann(m-1, ackermann(m, n-1));
}
}

int main() {
int m, n;

printf("Enter the value of m and n: ");


scanf("%d %d",&m,&n);

printf("Value returned by the function: %d",ackermann(m,n));

return 0;
}
Output:

Q3) Write a C program to find the index of the maximum element in


an array of integers. The program should return the index of the first
occurrence of the maximum element. int find_max_index(int arr[], int
size);
#include <stdio.h>
#include <stdlib.h>

int find_max(int arr[], int n) {


int a = arr[0];
int index = 0;
for(int i = 1; i < n; i++) {
if(arr[i] > a) {
a = arr[i];
index = i;
}
}
return index;
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));

printf("Enter %d elements of the array: ", n);


for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Index of maximum element: %d", find_max(arr, n));

free(arr);

return 0;
}
Output:

Q4) Write a C program to implement binary search, mentioning the


key to be searched. int binarySearch(int arr[], int size, int key);
#include <stdio.h>
#include <stdlib.h>

int binarySearch(int arr[], int n, int key){


if (n==0) {
return -1;
}

int mid=n/2;

if (n==1) {
if(arr[0]==key)
return 1;
else
return -1;
}
if(arr[mid]==key) {
return 1;
}
else if(arr[mid]>key){
return binarySearch(arr,mid,key);
}
else if(arr[mid]<key){
return binarySearch(arr+mid+1,mid,key);
}
}

int main() {
int n, key;

printf("Enter size of the array: ");


scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));

printf("Enter %d elements of the array: ", n);


for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the key to search: ");


scanf("%d", &key);

if(binarySearch(arr,n,key)==1) {
printf("Element found!");
}
else if(binarySearch(arr,n,key)==-1){
printf("Element not found!");
}

free(arr);

return 0;
}
Output:

Q5) The Tower of Hanoi is a mathematical puzzle consisting of three


rods and a number of disks of different sizes. The puzzle starts with
the disks stacked in increasing order of size on one rod, and the
objective is to move the entire stack to another rod, following these
rules: 1. Only one disk can be moved at a time. 2. Each move consists
of taking the upper disk from one stack and placing it on top of
another stack. 3. No disk may be placed on top of a smaller disk. You
need to write a program to solve the Tower of Hanoi problem and
print the sequence of moves
#include <stdio.h>
void toh(int n, char from, char to, char aux){
if(n==1){
printf("Move disk %d from Rod %c to Rod %c\n",n,from,to);
return;
}
toh(n-1,from,aux,to);
printf("Move disk %d from Rod %c to Rod %c\n", n, from, to);
toh(n-1,aux,to,from);
}

int main() {
int n;

printf("Enter number of disks: ");


scanf("%d",&n);

char from = 'A';


char to = 'B';
char aux = 'C';

toh(n, from, to, aux);

return 0;
}
Output:

Lab Assignment 10
SELECTION SORT :

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

void selection(int sort[],int n) {


int t;

for(int i=0;i<n-1;i++){
for(int j=i;j<n;j++){
if(sort[i]>sort[j]){
t = sort[i];
sort[i] = sort[j];
sort[j] = t;
}
}
}
printf("Sorted array is ");
for(int i=0; i<n; i++){
printf("%d ",sort[i]);
}

int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d",&n);

int* arr=(int*)malloc(n*sizeof(int));
printf("Enter the elements of the array: ");
for(int i=0; i<n; i++){
scanf("%d",&arr[i]);
}

selection(arr,n);

return 0;
}
BUBBLE SORT :

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

void bubble(int sort[],int n) {


int t;

for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(sort[j]>sort[j+1]){
t = sort[j];
sort[j]=sort[j+1];
sort[j+1]=t;
}
}
}
printf("Sorted array is ");
for(int i=0; i<n; i++){
printf("%d ",sort[i]);
}

int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d",&n);

int* arr=(int*)malloc(n*sizeof(int));
printf("Enter the elements of the array: ");
for(int i=0; i<n; i++){
scanf("%d",&arr[i]);
}

bubble(arr,n);

return 0;
}

INSERTION SORT :

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

void insertion(int sorted[], int unsorted[], int n) {


sorted[0] = unsorted[0];

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


int t = unsorted[i];
int j = i - 1;

while (j >= 0 && sorted[j] > t) {


sorted[j + 1] = sorted[j];
j--;
}
sorted[j + 1] = t;
}
printf("Sorted array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", sorted[i]);
}
printf("\n");
}

int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);

int* sorted = (int*)malloc(n * sizeof(int));


int* unsorted = (int*)malloc(n * sizeof(int));

printf("Enter the elements of the array: ");


for (int i = 0; i < n; i++) {
scanf("%d", &unsorted[i]);
}
insertion(sorted, unsorted, n);

return 0;
}

MERGE SORT :

#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int mid, int r) {
int n1 = mid - l + 1;
int n2 = r - mid;

int *left = (int *)malloc(n1 * sizeof(int));


int *right = (int *)malloc(n2 * sizeof(int));

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


left[i] = arr[l + i];
for (int i = 0; i < n2; i++)
right[i] = arr[mid + 1 + i];

int i = 0;
int j = 0;
int k = l;

while (i < n1 && j < n2) {


if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
}
else {
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}

while (j < n2) {


arr[k] = right[j];
j++;
k++;
}
}

void mergesort(int arr[], int l, int r) {


int mid;
if(l>=r)
return;
else {
mid=(l+r)/2;
mergesort(arr,l,mid);
mergesort(arr,mid+1,r);
merge(arr,l,mid,r);
}
}

int main()
{
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));

printf("Enter the elements of the array: ");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

mergesort(arr,0,n-1);

for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}

return 0;
}

QUICK SORT :
#include <stdio.h>
#include <stdlib.h>

int partition(int arr[], int l, int r) {


int pivot = arr[l];

int i = l;

for (int j = l + 1; j <= r; j++) {

if (arr[j] < pivot) {


i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

int temp = arr[l];


arr[l] = arr[i];
arr[i] = temp;

return i;
}

void quicksort(int arr[], int l, int r)


{
int x;
if(l>=r)
return;
else
{
x=partition(arr,l,r);
quicksort(arr,l,x-1);
quicksort(arr,x+1,r);
}
}

int main()
{
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));

printf("Enter the elements of the array: ");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

quicksort(arr,0,n-1);

for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
return 0;
}

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) Create a structure to specify data on students given below:


Roll number, Name, Department, Course, Year of joining
Assume that there are not more than 250 students in the college
(a) Write a function to print names of all students who joined in a
particular year.
(b) Write a function to print data of a student whose roll number is
given.
#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.) Create a structure to specify data of customers in a bank. The
data to be stored is : Account number, Balance in account. Assume
maximum of 200 customers in the bank..
(a) Write a function to print the account number and name of each
customer with balance below Rs. 100.
(b) If a customer requests for withdrawal or deposit, it is given in
the form: Account number, amount, code(1 for deposit, 0 for
withdrawal)
Write a program to give a message, “ the balance is insufficient for the
specified withdrawal”.
#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.) Create a structure named employee that hold information like
empcode, name and date of joining. Write a program to create an
array of structures and enter some data into it. Then ask the user to
enter current date. Display the name of those employee whose tenure
is 3 or more than 3 years according to the current date.
#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.) Write a menu driven program that depicts the working of a
library. The menu option would be:
1. Add book information
2. Display book information
3. List all book of given author
4. List the title of specified book
5. List the count of book in the library
6. list the books in the order of accession number
7. exit
Create a structure called library to hold accession number, title of the
book, author name, price of the book and flag indicating whether
book is issued or not.
#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.

Lab Assignment 12
Q1.) Implement stack using array
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 5

struct Stack {
int items[MAX_SIZE];
int top;
};

int overflow(struct Stack* sPtr) {


return sPtr->top == MAX_SIZE - 1;
}
void push(struct Stack* sPtr, int item) {
if (overflow(sPtr)){
printf("\n OVERFLOW STACK FULL \n");
}
else{
sPtr->top++;
sPtr->items[sPtr->top] = item;
printf("\nItem pushed: %d\n", item);
}
}
int pop(struct Stack* sPtr) {
if (sPtr->top == -1) {
printf("\nUNDERFLOW Stack is empty\n");
return -1;
}
else {
int item = sPtr->items[sPtr->top];
sPtr->top--;
return item;
}
}
int main() {
int item;
unsigned int choice = 0;
struct Stack s;
struct Stack* sPtr = &s;
sPtr->top = -1;

printf("\n Stack implementation using array\n");

while (choice != 3) {
printf("\n****************************\n");
printf("\nEnter your choice:\t1 Push \t2 Pop \t3 End\t: ");

if (scanf("%d", &choice) != 1) {
printf("\nInvalid input. Please enter a number.\n");
while (getchar() != '\n');
continue;
}

switch (choice) {
case 1:
printf("Enter an integer data: ");
if (scanf("%d", &item) == 1) {
push(sPtr, item);
}
else {
printf("\nInvalid input. Please enter an integer.\n");
}
break;
case 2:
{
int poppedItem = pop(sPtr);
if (poppedItem != -1) {
printf("Item popped: %d\n", poppedItem);
}
}
break;
case 3:
printf("Exiting program...\n");
break;

default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
Output.
Q2.) Implement queue using stack

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

#define MAX 10

struct Queue {
int arr[MAX];
int top;
};
int isEmpty(struct Queue* queue) {
return queue->top == -1;
}

int isFull(struct Queue* queue) {


return queue->top == MAX - 1;
}

void enqueue(struct Queue* queue, int value) {


if (isFull(queue)) {
printf("Queue is full! Cannot enqueue.\n");
return;
}
queue->arr[++queue->top] = value;
printf("Enqueued %d\n", value);
}

int dequeue(struct Queue* queue) {


if (isEmpty(queue)) {
printf("Queue is empty! Cannot dequeue.\n");
return -1;
}
int front = queue->arr[0];
for (int i = 1; i <= queue->top; i++) {
queue->arr[i - 1] = queue->arr[i];
}
queue->top--;
return front;
}

int main() {
struct Queue queue;
int choice, value;

queue.top = -1;

do {
printf("\nQueue Operations Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&queue, value);
break;
case 2:
value = dequeue(&queue);
if (value != -1) {
printf("Dequeued element: %d\n", value);
}
break;
case 3:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 3);

return 0;
}

Output.

Q3.) Check whether a string is palindrome or not

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

int main() {
char str[100];
printf("Enter a string: ");
scanf("%[^\n]s",str);

int length = strlen(str);

int pal = 1;

for (int i = 0; i < length / 2; i++) {


if (str[i] != str[length - i - 1]) {
pal = 0;
break;
}
}

if (pal) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}

return 0;
}

Output.
Q.4) Check whether a number is palindrome or not.
#include <stdio.h>

int main() {
int num, reverse = 0, org, rem;

printf("Enter a number: ");


scanf("%d", &num);

org = num;

while (num != 0) {
rem = num % 10;
reverse = reverse * 10 + rem;
num = num / 10;
}

if (org == reverse) {
printf("The number is a palindrome.\n");
} else {
printf("The number is not a palindrome.\n");
}
return 0;
}
Output.

Q5.) To find transpose of a given 2D array


#include <stdio.h>

int main() {
int rows, cols;

printf("Enter number of rows: ");


scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

int matrix[rows][cols], transpose[cols][rows];

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

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


for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

printf("\nOriginal Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

printf("\nTranspose of the Matrix:\n");


for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}

return 0;
}
Output.

Q6.) To add, subtract and multiply two 2D arrays.


#include <stdio.h>

#define MAX 5

void add(int a[MAX][MAX], int b[MAX][MAX], int result[MAX]


[MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}

void sub(int a[MAX][MAX], int b[MAX][MAX], int result[MAX]


[MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = a[i][j] - b[i][j];
}
}
}

void multi(int a[MAX][MAX], int b[MAX][MAX], int result[MAX]


[MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = 0;
for (int k = 0; k < cols; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}

void printMatrix(int mat[MAX][MAX], int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main() {
int a[MAX][MAX], b[MAX][MAX], result[MAX][MAX];
int rows, cols;
int choice;

printf("Enter the number of rows and columns (max 5): ");


scanf("%d %d", &rows, &cols);

printf("Enter elements of the first matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of the second matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &b[i][j]);
}
}

do {
printf("\nMenu:\n");
printf("1. Add matrices\n");
printf("2. Subtract matrices\n");
printf("3. Multiply matrices\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
add(a, b, result, rows, cols);
printf("Sum of the matrices:\n");
printMatrix(result, rows, cols);
break;
case 2:
sub(a, b, result, rows, cols);
printf("Difference of the matrices:\n");
printMatrix(result, rows, cols);
break;
case 3:
if (rows == cols) {
multi(a, b, result, rows, cols);
printf("Product of the matrices:\n");
printMatrix(result, rows, cols);
} else {
printf("Matrix multiplication requires square matrices
(rows == cols).\n");
}
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);

return 0;
}

Output.

You might also like