Index: Divesh Puri 7CS3,538 A2305207145
Index: Divesh Puri 7CS3,538 A2305207145
Divesh Puri
7CS3,538
A2305207145
Signatur Remark
SN Date Program Name e s
WAP to obtain GCD of two numbers
1 recursively
WAP to implement Binary Search
2 Recursively
3 WAP to implement Tower of Hanoi problem
4 WAP to implement Quick Search Algorithm
5 WAP to implement Merge Sort Algorithm
6 WAP to implement PRIM’s Algorithm
7 WAP to implement KRUSKAL’s Algorithm
WAP to demonstrate 4-Queen’s problem
8 Algorithm
WAP to demonstrate 8-Queen’s problem
9 Algorithm
10 WAP to implement Dijkstra’s Algorithm
11 WAP to demonstrate DES Technique
12 WAP to demonstrate BFS Technique
Program No. # 1
#include<iostream>
int main()
{
int a,b;
cout<<"Enter two numbers for GCD : ";
cout<<"\n1. ";
cin>>a;
cout<<"\n2. ";
cin>>b;
if(a<0 || b<0)
cout<<"The GCD is : "<<(-1)*gcd(a,b);
else if(a<0 && b<0 || a>0 && b>0)
cout<<"The GCD is : "<<gcd(a,b);
return 0;
}
/*
OUTPUT
path>gcd.out
Enter two numbers for GCD
1. 30
2. 45
The GCD is : 30
path>gcd.out
Enter two numbers for GCD
1. -30
2. -45
The GCD is : 30
path>gcd.out
Enter two numbers for GCD
1. -30
2. 45
The GCD is : 30
path>gcd.out
Enter two numbers for GCD
1. -30
2. -45
The GCD is : 30
*/
Program No. #2
#include<iostream>
int main()
{
int a[10],i,item;
void binary(int x[10],int beg,int end,int y);
cout<<"Binary Search for 10 numbers "<<endl;
for(i=0;i<10;i++)
{
cout<<"enter the "<<i+1<<"no.:";
cin>>a[i];
}
binary(a,0,9,item);
return 0;
}
if(beg<=end)
{
mid=(beg+end)/2;
if(x[mid]==y)
{
cout<<"number found"<<endl;
}
if(y<x[mid])
{
end=mid-1;
binary(x,beg,end,y);
}
else
{
beg=mid+1;
binary(x,beg,end,y);
}
}
else
cout<<"Number not found";
}
/*
OUTPUT
path>g++ bin_search_re.cpp -o bin_search_re.out
path>bin_search_re.out
Binary Search for 10 numbers
enter the 1no.:1
enter the 2no.:2
enter the 3no.:3
enter the 4no.:4
enter the 5no.:5
enter the 6no.:6
enter the 7no.:7
enter the 8no.:8
enter the 9no.:9
enter the 10no.:0
path>bin_search_re.out
Binary Search for 10 numbers
enter the 1no.:1
enter the 2no.:2
enter the 3no.:3
enter the 4no.:4
enter the 5no.:5
enter the 6no.:6
enter the 7no.:7
enter the 8no.:8
enter the 9no.:9
enter the 10no.:0
if(disk == 1)
{
cout<<"Move disk from "<<from<<" to "<<to<<endl;
}
else
{
hanoi(disk-1,from,aux,to);
cout<<"Move disk from "<<from<<" to "<<to<<endl;
hanoi(disk-1,aux,to,from);
}
}
int main()
{
int disk;
double moves;
moves = pow(2,disk)-1;
cout<<"\nThe number of moves are : "<<moves<<endl;
hanoi(disk,'A','C','B');
return 0;
}
/*
OUTPUT
path>./tower_of_hanoi
path>./tower_of_hanoi
Enter the number of Disks : 4
After Sorting :
-50
-1
0
1
4
5
5
43
88
99
path>
Program No. #5
#include<iostream>
void merge(int numbers[], int temp[], int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;
else
{
temp[tmp_pos] = numbers[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}
{
m_sort(numbers, temp, 0, array_size - 1);
int main()
{
int array[50],temp[50],n,i;
for(i=0;i<n;i++)
{
cout<<"Enter the "<<i+1<<" element : ";
cin>>array[i];
}
mergeSort(array,temp,n);
return 0;
}
OUTPUT
path>g++ MergeSort.cpp -o MergeSort
path>MergeSort
Enter the size of the array : 8
Enter the 1 element : 5
Enter the 2 element : 2
Enter the 3 element : 4
Enter the 4 element : 6
Enter the 5 element : 1
Enter the 6 element : 3
Enter the 7 element : 2
Enter the 8 element : 6
The Array after Sorting
The element at 1 is : 1
The element at 2 is : 2
The element at 3 is : 2
The element at 4 is : 3
The element at 5 is : 4
The element at 6 is : 5
The element at 7 is : 6
The element at 8 is : 6
path>