c file
c file
c 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;
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);
}
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;
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++;
}
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;
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;
}
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 main()
{
int a, b;
printf("Sum = %d",add(a,b));
return 0;
}
Output:
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:
int main()
{
int x,y;
swap(&x,&y);
int main() {
int marks[10], n;
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];
int main()
{
int arr[5];
reverseArray(arr, 5);
return 0;
}
Output:
int main()
{
int arr[3];
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
int main()
{
int arr[5];
displayArray(arr, 5);
return 0;
}
Q8[array, function, pointer]:Compare Two Arrays using a Function
#include<stdio.h>
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]);
}
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:
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:
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;
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:
int main() {
char key;
char arr[100];
readString(arr);
printString(arr);
return 0;
}
Output:
b)
#include <stdio.h>
int main() {
char key;
char arr[100];
readString(arr);
printString(arr);
return 0;
}
Output:
int main() {
int n;
return 0;
}
Output:
b)
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
reverse_array(arr, n);
free(arr);
return 0;
}
Output:
c)
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
reverse_array(arr, n);
free(arr);
return 0;
}
Output:
d)
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
reverse_array(arr, n);
free(arr);
return 0;
}
Output:
int main() {
int n;
printf("Enter size of the array: ");
scanf("%d", &n);
free(arr);
return 0;
}
Output:
b)
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter size of the array: ");
scanf("%d", &n);
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;
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 main() {
int m, n;
return 0;
}
Output:
int main() {
int n;
free(arr);
return 0;
}
Output:
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;
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:
int main() {
int n;
return 0;
}
Output:
Lab Assignment 10
SELECTION SORT :
#include <stdio.h>
#include <stdlib.h>
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>
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>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &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 i = 0;
int j = 0;
int k = l;
int main()
{
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
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 i = l;
return i;
}
int main()
{
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
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;
};
return 0;
}
Output:
#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.
struct student {
char rollno[10];
char name[50];
char department[8];
char course[20];
int year_of_joining;
}std[250];
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];
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;
};
int main() {
struct customer customers[MAX_CUSTOMERS];
int n, acc_number, code;
float amount;
printf("Name: ");
scanf(" %[^\n]s",customers[i].name);
printf("Balance: ");
scanf("%f", &customers[i].balance);
}
print_low_balance(customers, n);
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);
printf("Name: ");
scanf(" %[\n]s",employees[i].name);
struct library {
int accession_number;
char title[50];
char author[50];
float price;
int is_issued;
};
(*count)++;
printf("Book added successfully!\n");
}
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);
}
}
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);
}
}
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;
};
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 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.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%[^\n]s",str);
int pal = 1;
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;
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.
int main() {
int rows, cols;
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");
}
return 0;
}
Output.
#define MAX 5
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.