4 PDS - LAB - Record
4 PDS - LAB - Record
EX.NO: 1(a)
DATE:
AIM:
To write a C program to convert the Celsius into Fahrenheit and Fahrenheit to Celsius
ALGORITHM:
Step 1: Start
Step 2: Read temperature in celsius.
Step 3: Calculate Fahrenheit = (temp_c*1.8)+32
Step 4: Print the Fahrenheit.
Step 5: Read temperature in Fahrenheit
Step 6: Calculate Fahrenheit = (temp_f-32) / 1.8
Step 7: Print the Fahrenheit value.
Step 8: Stop
PROGRAM:
#include<stdio.h>
int main ()
{
float temp_c, temp_f;
OUTPUT:
RESULT:
Thus C program to convert the Celsius into Fahrenheit and Fahrenheit into Celsius is
executed and the output is verified.
DATE:
AIM:
To write a C program to find the largest of three numbers.
ALGORITHM:
Step 1: Start
Step 2: Read a , b ,c.
Step 3: Check if(a>b) and if (a>c) then max=a.
Step 4: Check if(b>c) and if (b>a) then max=b.
Step 5: else max=c.
Step 5 : Then print maximum value .
Step 6 : Stop.
PROGRAM:
#include <stdio.h>
int main()
{
int a,b,c,max;
printf("enter three values");
scanf("%d %d %d",&a,&b,&c);
if (a>b)
{
if(a>c)
max=a;
else
max=c;
}
else
{
if(b>c)
max=b;
else
max=c;
}
printf("Maximum number is %d " , max);
return 0;
}
OUTPUT:
Enter three values 56 76 11
Maximum number is 76
RESULT:
Thus the C program to find the largest of three numbers is executed and the output is
verified.
DATE:
AIM:
ALGORITHM:
Step 1: Start.
Step 2: Read the choice.
Step 3: Using switch case print the day using the entered choice starting form 0 = Sunday till
6 = Saturday.
Step 4: else print invalid day.
Step 5: Stop.
PROGRAM:
#include <stdio.h>
#include <conio.h>
int main()
{
int ch ;
printf("Enter the Day number (Starting from 0 = Sunday) : ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 0:
printf("\nSunday") ;
break ;
case 1:
printf("\nMonday") ;
break ;
case 2:
printf("\nTuesday") ;
break ;
case 3:
printf("\nWednesday") ;
break ;
case 4:
printf("\nThursday") ;
break ;
case 5:
printf("\nFriday") ;
break ;
case 6:
printf("\nSaturday") ;
break ;
default:
printf("INVALID DAY") ;
}
getch();
return 0;
}
OUTPUT:
Enter the Day number (Starting from 0 = Sunday): 1
Monday
RESULT:
Thus the C program to print the day of a week using switch case is executed and the
output is verified.
AIM:
ALGORITHM:
Step 1: Start.
Step 2: Read N value.
Step 3: Check if((i%2==0)&&(i%3!=0)&&(i%5!=0)) then print i
Step 4: Stop.
PROGRAM:
#include<stdio.h>
int main()
{
int i,n;
printf("Enter n value : ");
scanf("%d",&n);
printf("\nThe numbers divisible by 2 not by 3 and 5 till %d are :\n",n);
for(i=1;i<=n;i++)
{
if((i%2==0)&&(i%3!=0)&&(i%5!=0))
{
printf(" %d",i);
}
}
return 0;
}
OUTPUT:
Enter n value: 100
2 4 8 14 16 22 26 28 32 34 38 44 46 52 56 58 62 64 68 74 76 82 86 88 92 94 98
RESULT:
Thus the C program to print numbers divisible by 2 but not by 3 and 5 is executed and
the output is obtained.
DATE:
WHILE LOOP
AIM:
To write a C program to find the sum of n natural numbers using while loop.
ALGORITHM:
Step 1: Start
Step 2: Read n .
Step 3: Initialize sum as 0 and the value of i as 1 .
Step 4: Iterate a while with a condition (i<=n) and update the value of sum = sum + i and
i++
Step 5: Print the value of sum.
Step 6: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n = 0 , sum = 0 , i = 1 ;
printf("Enter n value : ") ;
scanf("%d",&n) ;
while (i<=n)
{
sum += i ;
i++;
}
printf("\n1+......+%d = ",n) ;
printf("%d",sum);
getch();
}
OUTPUT:
Enter n value: 10
1+......+10 = 55
RESULT:
Thus the C program to find the sum of n natural numbers is executed and the output
is verified.
EX.NO: 2 (a)
DATE:
C PROGRAM TO CHECK WHETHER THE GIVEN STRING IS
PALINDROME OR NOT
AIM:
ALGORITHM:
Step 1: Start
Step 2: Read the string from the user.
Step 3: Take a copy of the string.
Step 4: Reverse the copied string.
Step 5: If they have zero difference then print “Palindrome” else print “Not a palindrome”.
Step 6: Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str1[30], str2[30];
printf("Enter a string: ");
scanf("%s",str1);
strcpy(str2, str1);
strrev(str2);
if(!strcmp(str1, str2))
printf("\n It's a Pallindrome!\n");
else
printf("\nIt's not a Pallindrome.\n");
return 0;
}
OUTPUT:
Enter a string: level
It's a Pallindrome!
RESULT:
Thus the C program to check the given string is palindrome or not is executed and the
output is verified.
EX.NO: 2(b)
DATE:
AIM:
To write a C program to swap two numbers using call by value and call by reference.
ALGORITHM:
Step 1: Start.
Step 2: Read two values a and b
Step 3: swap using t=a; a=b; b=t by value
Step 4: swap using t=*a; *a=*b; *b=t; by address
Step 5: After swapping print values of a and b
Step 6: Stop.
PROGRAM:
#include <stdio.h>
int swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("\n After Swap - Inside function A=%d and B=%d",a,b);
}
int main()
{
int x,y;
printf("Enter two value");
scanf("%d %d",&x,&y);
return 0;
}
OUTPUT:
Call by Value
*****************
Before Swap - Main function A=5 and B=6
After Swap - Inside function A=6 and B=5
After Swap - Main function A=5 and B=6
Call by Reference
*****************
Before Swap - Main function A=5 and B=6
After Swap - Inside function A=6 and B=5
After Swap - Main function A=6 and B=5
RESULT:
Thus C program to swap two numbers using call by value and call by reference
is executed and the output is verified.
EX.NO: 2(c)
DATE:
AIM:
To write a C program to calculate the factorial of a given number using recursion.
ALGORITHM:
Step 1: Start
Step 2: Read n.
Step 3:if n=0 or n=1 print factorial as 1
else call the recursive function by passing the user input as the argument.
Step 4: Print the factorial of the number.
Step 5: Stop.
PROGRAM:
#include<stdio.h>
int fact(int n)
{
if (n==1)
return 1;
else
return n * fact (n-1);
}
int main()
{
int n;
printf("Enter Number: ");
scanf("%d",&n);
printf("%d ! = %d", n, fact(n));
return 0;
}
OUTPUT:
Enter Number: 5
5 ! = 120
RESULT:
EX.NO: 2(d)
DATE:
AIM:
ALGORITHM:
Step 1: Start
Step 2: create structure for storing the student details.
Step 3: Read two subject marks
Step 4: Calculate total and average and print the same.
Step 5: Stop
PROGRAM:
#include<stdio.h>
struct stu
{
int rollno, s1, s2, tot ;
char name[10] ;
float avg ;
} s[10] ;
int main()
{
int i, n ;
return 0;
}
OUTPUT:
RESULT:
Thus the C program to calculate the total and average using structure is
executed and the output is verified.
EX.NO: 3
DATE:
AIM:
To write a c program to print student details using sequential access file.
ALGORITHM:
Step 1: Start
Step 2: Open a file *fp
Step 3: Read rollno, name and marks
Step 4: calculate total and average of marks.
Step 4: write all the contents into file named student.txt
Step 6: Print the student details from student.txt file
Step 7: Stop
PROGRAM:
#include<stdio.h>
struct stu
{
int rollno, s1, s2, tot ;
char name[10] ;
float avg ;
}s[10];
int main()
{
FILE *f;
int i,n;
clrscr();
f=fopen("student.txt","w");
if(f==NULL)
{
printf("\n Error opening student.txt\n\n");
exit(1);
}
printf("Enter the number of students : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the roll number : ") ;
scanf("%d", &s[i].rollno) ;
printf("\nEnter the name : ") ;
scanf("%s", s[i].name) ;
21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE
14
RESULT:
Thus the C program to find the total and average for two subject marks using
sequential access file is executed and verified.
EX.NO: 4(a)
DATE:
AIM:
ALGORITHM:
Step 1: Include all the header files which are used in the program and define a constant
'SIZE' with specific value.
Step 3: Create a one dimensional array with fixed size (int stack[SIZE])
Step 4: Define an integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5: In main method, display menu with list of operations and make suitable function calls
to perform operation selected by the user on the stack.
PROGRAM
#include<stdio.h>
#define SIZE 10
int stack[SIZE], top = -1;
int display()
{
if(top == -1)
printf("\nStack is Empty!!!");
else
{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}
int main()
{
int value, choice;
while(1)
{
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4:
exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
OUTPUT
4. Exit
RESULT:
Thus the C program for array implementation of Stack ADT is executed and the
output is obtained.
EX.NO: 4 (b)
DATE:
AIM:
To create a C program to implement queue using array.
ALGORITHM:
Step 1 - Include all the header files which are used in the program and define a constant 'n'
with specific value.
Step 2 - Declare all the user defined functions which are used in queue implementation.
Step 3 - Create a one dimensional array with above defined n (int queue[n])
Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '0’.
Step 5 - Then implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on queue.
PROGRAM
#include<stdio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
printf("\n Queue is empty");
else
{
RESULT:
Thus the C program for implementation of queue using array is successfully executed and the
output is verified.
EX.NO: 5(a)
DATE:
AIM:
ALGORITHM:
Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.
Step 2 - Define a 'Node' structure with two members data and next.
Step 4 - Implement the main method by displaying Menu with list of operations and make
suitable function calls in the main method.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice, value;
printf("\n Stack using Linked List \n");
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
while(1)
{
printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct Node *temp = top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
OUTPUT
RESULT:
Thus the C program to print linked list implementation of Stack ADT is executed and
the output is verified.
EX.NO: 5 (b)
DATE:
AIM:
ALGORITHM:
Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.
Step 2 - Define a 'Node' structure with two members data and next.
Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4 - Implement the main method by displaying Menu of list of operations and make
suitable function calls in the main method to perform user selected operation.
PROGRAM
#include<stdio.h>
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void insert(int);
void delete();
void display();
void main()
{
int choice, value;
printf("\n Queue Implementation using Linked List \n");
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
while(1)
{
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else
{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}
}
OUTPUT
RESULT:
Thus, the C program to print array implementation of Queue ADT is executed and the
output is verified.
EX.NO: 6
DATE:
AIM:
ALGORITHM:
1. Start
5. Write Delete function is use to delete the value from the tree.
7. Stop
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *left_child, *right_child;
};
if (root_node != NULL)
{
print(root_node->left_child);
printf("%d -->", root_node->value);
print(root_node->right_child);
}
}
int main()
{
struct node *root = NULL;
int data;
char ch;
}
printf("\nDo you want to continue (Type y or n)\n");
scanf(" %c",&ch);
} while (ch == 'Y'|| ch == 'y');
return 0;
}
OUTPUT
1. Create Tree
2. Display
3. Delete
4. Exit
Enter ur choice1
Enter the value to be inserted
5
Do you want to continue (Type y or n)
Y
Enter ur choice1
Enter the value to be inserted
2
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
1
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
3
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
6
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
7
Do you want to continue (Type y or n)
y
Enter ur choice2
Construct Binary Tree::
1 -->2 -->3 -->5 -->6 -->7 -->
Do you want to continue (Type y or n)
y
Enter ur choice3
All data are removed
Do you want to continue (Type y or n)
y
Enter ur choice4
Wrong Entry
RESULT:
Thus the C program to create Binary Tree and various operations like, insert,
delete and display is obtained.
EX.NO: 7
DATE:
AIM:
ALGORITHM:
1. Start
8. Stop
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *left_child, *right_child;
};
//One Child
else if(root_node->left_child==NULL || root_node->right_child==NULL)
{
struct node *temp;
if(root_node->left_child==NULL)
temp = root_node->right_child;
else
temp = root_node->left_child;
free(root_node);
return temp;
}
//Two Children
else
{
struct node *temp = find_minimum(root_node->right_child);
root_node->value = temp->value;
root_node->right_child = delete(root_node->right_child, temp->value);
}
}
return root_node;
}
int main()
{
struct node *root = NULL;
int data;
char ch;
printf("\n 1.Create Tree \n 2.Construct Tree \n 3.Delete \n 4.Search \n 5.Exit");
do
{
int choice;
printf("\n Enter ur choice");
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\n Construct Tree::\n");
print(root);
break;
case 3 :
printf("\nEnter the value to be deleted\n");
scanf("%d",&data);
root=delete(root,data);
print(root);
break;
case 4:
printf("\n Enter the value to be searched");
scanf("%d",&data);
root=search(root,data);
print(root);
break;
default :
printf("Wrong Entry\n");
break;
}
printf("\nDo you want to continue (Type y or n)\n");
scanf(" %c",&ch);
} while (ch == 'Y'|| ch == 'y');
return 0;
}
OUTPUT
1. Create Tree
2. Construct Tree
3. Delete
4. Search
5. Exit
Enter ur choice1
Enter the value to be inserted
5
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
2
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
1
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
3
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
6
Do you want to continue (Type y or n)
y
Enter ur choice1
Enter the value to be inserted
7
Do you want to continue (Type y or n)
y
Enter ur choice2
Construct Tree::
1 -->2 -->3 -->5 -->6 -->7 -->
Do you want to continue (Type y or n)
y
Enter ur choice3
Enter the value to be deleted
3
1 -->2 -->5 -->6 -->7 -->
Do you want to continue (Type y or n)
y
Enter ur choice4
Enter the value to be searched6
6 -->7 -->
Do you want to continue (Type y or n)
y
Enter ur choice5
Wrong Entry
RESULT:
Thus the C program of Binary Search Tree implementation is executed and
the output is verified.
EX.NO: 8
DATE:
AIM:
ALGORITHM:
8. Stop
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct node
int value;
};
tmp->value = value;
return tmp;
if (root_node)
inorder(root_node->left_child);
printf("%d-->",root_node->value);
inorder(root_node->right_child);
if (root_node)
postorder(root_node->left_child);
postorder(root_node->right_child);
printf("%d-->",root_node->value);
int main()
int data;
char ch;
do
int choice;
scanf("%d",&choice);
switch (choice)
case 1 :
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
inorder(root);
break;
case 3 :
preorder(root);
break;
case 4 :
postorder(root);
break;
default :
printf("Wrong Entry\n");
break;
scanf(" %c",&ch);
return 0;
OUTPUT
1. Create Tree
2. Inorder Traversal
3. Preorder Traversal
4. Postorder Traversal
5. Exit
Enter ur choice1
Enter ur choice1
Enter ur choice1
Enter ur choice1
Enter ur choice1
Enter ur choice1
Enter ur choice2
Inorder Traversal::
1-->2-->3-->5-->6-->7-->
Enter ur choice3
Preorder Traversal::
5-->2-->1-->3-->6-->7-->
Enter ur choice4
Postorder Traversal::
1-->3-->2-->7-->6-->5-->
Enter ur choice5
Wrong Entry\
RESULT:
Thus the above C program to implement Binary Tree Traversal implementation is executed
and the output is verified.
EX.NO: 9(a)
DATE:
AIM
To write a C program for implementation of Breadth First Search (BFS) in graph
using adjacency matrix.
ALGORITHM
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.
PROGRAM
#include<stdio.h>
void BFS(int);
int graph[10][10], visited[10],total;
main()
{
int i,j;
printf("\nEnter the total number of vertices in graph\n");
scanf("%d",&total);
{
int j;
printf("%d\t",vertex);
visited[vertex] = 1;
for(j=0;j<total;j++)
{
if(!visited[j] && graph[vertex][j] == 1 )
{
BFS(j);
}
}
}
OUTPUT
BFS traversal is
0 1 3 2 4
RESULT:
Thus the C program to implement the graph traversal (BFS) is executed and output is
verified.
EX.NO: 9(B)
DATE:
AIM
To implement Depth First Search (DFS) in graph using adjacency matrix.
ALGORITHM
#include<stdio.h>
#include<stdlib.h>
int graph[10][10], visited[10],total,arr[30];
static int k=0,count=0;
void DFS(int);
main()
{
int i,j;
printf("\nEnter the total number of vertices in graph\n");
scanf("%d",&total);
printf("\nEnter the adjacency matrix\n");
for(i=0;i<total;i++)
{
for(j=0;j<total;j++)
{
scanf("%d",&graph[i][j]);
}
}
for(i=0;i<total;i++)
{
visited[i] = 0;
}
printf("\nDFS traversal is \n");
DFS(0);
}
void DFS(int vertex)
{
int j,c=0;
count++;
printf("%d\t",vertex);
visited[vertex] = 1;
for(j=0;j<total;j++)
{
if(!visited[j] && graph[vertex][j] == 1)
{
arr[++k] = j;
c=1;
}
21CS1211 – PROGRAMMING AND DATA STRUCTURES LAB II SEM- CSE
44
if(count == total)
{
exit(0);
}
}
if(c==1)
{
DFS(arr[k]);
}
else
{
k--;
DFS(arr[k]);
}
}
OUTPUT
DFS traversal is
0 3 4 2 1
RESULT:
Thus the C program to implement the graph traversal (DFS) is executed and the
output is verified.
EX.NO: 10
DATE:
AIM:
To write a c program to implement hash table.
ALGORITHM:
PROGRAM:
#include <stdio.h>
struct pair
{
int key,value;
};
int main()
{
int size , i , numbers;
OUTPUT
key value
0 10
1 11
2 22
3 33
4 44
RESULT:
Thus the C program to implement hash table is executed and the output is verified.
EX.NO: 11(a)
DATE:
AIM:
To write a c program to implement linear search.
ALGORITHM:
Step 1: First, read the search element (Target element) in the array.
Step 2: In the second step compare the search element with the first element in the
array.
Step 3: If both are matched, display "Target element is found" and terminate the
Linear Search function.
Step 4: If both are not matched, compare the search element with the next element in
the array.
Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is compared
with the last element of the array.
Step 6 - If the last element in the list does not match, the Linear Search Function will
be terminated, and the message "Element is not found" will be displayed.
PROGRAM
#include <stdio.h>
int main()
{
int i,size,a[10],search;
printf("\n Enter the size of an array :");
scanf("%d",&size);
printf("\n Enter the value :");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\n Find the search element : ");
scanf("%d",&search);
for(i=0;i<size;i++)
{
if(search==a[i])
{
printf("%d fount at the position %d ",search,i+1);
break;
}
}
if (i==size)
printf("element not found");
return 0;
}
OUTPUT
----------
RESULT:
Thus the C program to implementation linear search is executed and the output is
verified.
EX.NO: 11(b)
DATE:
AIM:
To write a C program to implement binary search.
ALGORITHM:
PROGRAM
#include <stdio.h>
int main()
{
int i,size,a[10],search,start,mid,stop,flag=0;
start=0;
stop=size-1;
while(start<=stop)
{
mid=(start+stop)/2;
if(search==a[mid])
{
flag=1;
break;
}
else if (search<a[mid])
stop=mid-1;
else
start=mid+1;
}
if(flag==1)
printf("%d fount at the position %d ",search,mid+1);
else
printf("element not found");
return 0;
}
OUTPUT
------
RESULT:
Thus the C program for implementation of binary search is executed and the output is
verified.
EX.NO: 12(a)
DATE:
AIM:
To write a c program to implement bubble sorting.
ALGORITHM:
1. Starts from the first index: a[0] and compares the first and second element: a[0] and a[1]
2. If a[0] is greater than a[1], they are swapped
3. Similarly, if a[1] is greater than a[2], they are swapped
4. The above process continues until the last element a[n-1]
PROGRAM
#include <stdio.h>
int main()
{
int a[50],n,i,j,temp;
printf("Enter Size of an Array :");
scanf("%d",&n);
printf("Values are");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Ascending Order Using Bubble Sort \n");
for(i=0;i<n;i++)
printf("\t %d",a[i]);
return 0;
}
OUTPUT
Enter Size of an Array :8
Values are
25 57 48 37 12 92 86 33
RESULT:
Thus the C program to implementation bubble sort is executed and the output is
verified.
EX.NO: 12(b)
DATE:
AIM:
To write a C program to implement selection sort.
ALGORITHM:
PROGRAM
#include <stdio.h>
int main()
{
int a[100], n, i, j, min,temp;
printf("Enter number of elements \n");
scanf("%d", &n);
printf("Enter Values");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
min=i;
for(j = i + 1; j < n; j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("Sorted Array:\n");
for(i = 0; i < n; i++)
printf("%d \n", a[i]);
return 0;
}
OUTPUT
RESULT:
Thus the C program to implementation selection sort is executed and the output is
verified.