Daa Record
Daa Record
Daa Record
EX NO:1(A)
IMPLEMENTATION AND TIME ANALYSIS OF FACTORIAL USING
ITERATIVE METHOD
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,fact=1,n;
clrscr();
printf("Enter positive number:");
scanf("%d",&num);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of %d is %d",n,fact);
getch();
}
OUTPUT:
Enter a positive integer: 6
Factorial of 6 is 720
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:1(B)
IMPLEMENTATION AND TIME ANALYSIS OF FACTORIAL USING
RECURSIVE METHOD
PROGRAM:
#include<stdio.h>
#include<conio.h>
int factorial(int n);
void main()
{
int n,fact;
clrscr();
printf("Enter number");
scanf("%d",&n);
fact=factorial(n);
printf("Factorial of %d is %d",n,fact);
getch();
}
int factorial(int n)
{
if(n==0)
return 1;
else
return n*factorial(n-1);
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
OUTPUT:
Enter a positive integer: 5
Factorial of 5 is 120
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:1(C)
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,GCD,i;
clrscr();
printf("Enter two numbers:");
scanf("%d%d",&n1,&n2);
for(i=1;i<=n1 && i<=n2;++i)
{
if(n1%i==0 && n2%i==0)
GCD=i;
}
printf("GCD of %d and %d is %d",n1,n2,GCD);
getch();
}
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
OUTPUT:
Enter two numbers:60 24
GCD of 60 and 24 is 12
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:1(C)
PROGRAM:
#include<stdio.h>
#include<conio.h>
if (b!=0)
return gcd(b,a%b);
else
return a;
int main()
int a,b;
clrscr();
scanf(“%d%d”,&a,&b);
getch();
return 0;
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
OUTPUT:
Enter two numbers:15 10
GCD of 15 and 10 is 5
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:2(A)
#include<stdio.h.>
void main()
int n,s,a[10],k=0,i,c;
for(i=0;i<n;i++)
printf(“a[%d]=”,i);
scanf(%d”,&a[i]);
scanf(“%d”,&s);
for(i=0;i<n;i++)
if(a[i]==s)
c=i;
k++;
if(k==0)
else
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:2(B)
PROGRAM:
#inlcude<stdio.h>
void main()
int first,last,mid,size,i,element,list[100];
scanf(“%d”,&size);
printf(“enter elements:”);
for(i=0;i<size;i++)
scanf(“%d”,&list[i]);
scanf(“%d”,element);
first=0;
last=size-1;
mid=(first+last)/2;
while(first<=last)
if(list[mid]<element)
first=mid+1;
else if(list[mid]=element)
break;
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
else
last=mid-1;
mid=(first+last)/2;
if(first>last)
OUTPUT:
enter elements: 10 11 17 12 19
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:3(A)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
arr[k] = L[i];
i++;}
else
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
arr[k] = R[j];
j++;
k++
arr[k] = L[i];
i++;
k++;}
{arr[k] = R[j];
j++;
k++;
if (l < r)
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}}
int i;
printf("\n");
int main()
clrscr();
printArray(arr, arr_size);
printArray(arr, arr_size);
getch();
return 0;
OUTPUT:
-1 3 12 43 48
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:3(A)
#include <stdio.h>
#include <stdbool.h>
#define MAX 7
int i;
printf("=");
printf("=\n");
void display() {
int i;
printf("[");
printf("%d ",intArray[i]);
printf("]\n");
while(true) {
break;
else {
swap(leftPointer,rightPointer);
swap(leftPointer,right);
display();
return leftPointer;
if(right-left <= 0) {
return;
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
} else {
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
int main() {
display();
printline(50);
quickSort(0,MAX-1);
display();
OUTPUT:
5 6 7 12
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:4
for(i=0;i<n;i++)
scanf("%d",&weight[i]);
printf("enter the values of items:\n");
for(i=0;i<n;i++)
scanf("%d",&profit[i]);
printf("the optimal solution is:%d",knapsack(w,weight,profit,n));
getch();
}
OUTPUT:
enter the no.of.items:3
10
20
30
60
100
120
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:5(A)
PROGRAM:
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\nEnter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]< min)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
OUTPUT:
enter the number of nodes:6
enter the adjacency matrix:
031600
305030
150564
605002
036006
004260
edge 1: (1 3) cost:1
edge 2: (1 2) cost:3
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
edge 3: (2 5) cost:3
edge 4: (3 6) cost:4
edge 5: (6 4) cost:2
minimum cost:13
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:5(B)
{
for (i = 1, min = 999; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (cost[i][j] < min)
{
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
u = find(u);
v = find(v);
if (uni(u, v))
{
printf("%d edge (%d,%d) =%d\n", ne++, a, b, min);
mincost += min;
}
cost[a][b] = cost[b][a] = 999;
}
printf("\nMinimum cost = %d\n", mincost);
getch();
}
int find(int i)
{
while (parent[i])
i = parent[i];
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
return i;
}
int uni(int i, int j)
{
if (i != j)
{
parent[j] = i;
return 1;
}
return 0;
}
OUTPUT:
Kruskal’s algorithm in c
Enter the no. of vertices:
5
Enter the cost adjacency matrix:
04000
00210
00008
50000
0 0 0 10 0
The edges of Minimum Cost Spanning Tree are
1 edge (2,4) =1
2 edge (2,3) =2
3 edge (1,2) =4
4 edge (3,5) =8
Minimum cost = 15
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:6
break;
}
}
for (int i = 0; i < n && engaged[man] == 0; i++) {
int woman = preference[man][i];
if (matching[woman] == -1) {
matching[woman] = man;
engaged[man] = 1;
count++;
}
else
{
int otherMan = matching[woman];
if (prefers(woman, man, otherMan, preference)) {
matching[woman] = man;
engaged[man] = 1;
engaged[otherMan] = 0;
}
}
}
}
printf("Matching:\n");
for (int i = 0; i < n; i++) {
printf("Man %d is engaged to Woman %d\n", matching[i], i);
}
}
int main()
{
int n;
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
OUTPUT:
Enter the number of men/women (maximum 10): 4
Enter the preference list of men and women (in order of preference, separated by spaces):
Man 0: 3 2 1 0
Man 1: 0 1 2 3
Man 2: 2 1 3 0
Man 3: 3 0 1 2
Matching:
Man 1 is engaged to Woman 0
Man 0 is engaged to Woman 1
Man 2 is engaged to Woman 2
Man 3 is engaged to Woman 3
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:8
PROGRAM:
#include <stdio.h>
#include <string.h>
#define MAX_NODES 10
int n;
int m;
int graph[MAX_NODES][MAX_NODES];
char color[MAX_NODES][20];
int i;
return 0;
return 1;
char c[20];
int i;
if (node == n) {
return 1;
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
strcpy(c, color[i]);
if (canColor(node, c)) {
strcpy(color[node], c);
if (colorGraph(node + 1)) {
return 1;
strcpy(color[node], "");
return 0;
int main()
int i;
n = 5;
m = 3;
graph[0][1] = graph[1][0] = 1;
graph[0][2] = graph[2][0] = 1;
graph[1][2] = graph[2][1] = 1;
graph[1][3] = graph[3][1] = 1;
graph[2][3] = graph[3][2] = 1;
graph[3][4] = graph[4][3] = 1;
strcpy(color[0], "red");
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
strcpy(color[1], "blue");
strcpy(color[2], "green");
if (colorGraph(0)) {
} else {
return 0;
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:9
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ITEMS 100
#define MAX_WEIGHT 1000
typedef struct {
int value;
int weight;
} item_t;
int read_items(item_t *items, int max_items) {
int n = 0;
while (scanf("%d %d", &items[n].value, &items[n].weight) == 2) {
n++;
if (n >= max_items) {
break;
}
}
return n;
}
int bound(item_t *items, int n, int capacity, int level, int value, int weight)
{
int i;
int bound = value;
for (i = level; i < n && weight + items[i].weight <= capacity; i++) {
weight += items[i].weight;
bound += items[i].value;
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
}
if (i < n) {
bound += (capacity - weight) * items[i].value / items[i].weight;
}
return bound;
}
void search(item_t *items, int n, int capacity, int level, int value, int
weight, int *best_value) {
if (weight > capacity) {
return;
}
if (level == n) {
if (value > *best_value) {
*best_value = value;
}
return;
}
if (value + bound(items, n, capacity, level, value, weight) <= *best_value) {
return;
}
search(items, n, capacity, level + 1, value, weight, best_value);
search(items, n, capacity, level + 1, value + items[level].value, weight + items[level].weight,
best_value);
}
int main() {
int capacity, n, best_value;
item_t items[MAX_ITEMS];
printf("Enter the capacity of the knapsack: ");
scanf("%d", &capacity);
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
EX NO:10
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_N 10
int n, cost[MAX_N][MAX_N], min_cost = INT_MAX;
int visited[MAX_N] = {0}, path[MAX_N];
void tsp(int pos, int curr_cost, int lower_bound) {
if (pos == n) {
if (curr_cost + cost[path[pos-1]][path[0]] < min_cost) {
min_cost = curr_cost + cost[path[pos-1]][path[0]];
}
return;
}
for (int i = 0; i < n; i++) {
if (!visited[i]) {
int new_lb = lower_bound + cost[path[pos-1]][i];
if (new_lb < min_cost) {
int old_lb = lower_bound;
lower_bound = new_lb;
curr_cost += cost[path[pos-1]][i];
path[pos] = i;
visited[i] = 1;
tsp(pos+1, curr_cost, lower_bound);
visited[i] = 0;
curr_cost -= cost[path[pos-1]][i];
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
lower_bound = old_lb;
}
}
}
}
int main() {
printf("Enter the number of cities: ");
scanf("%d", &n);
printf("Enter the cost matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &cost[i][j]);
}
}
visited[0] = 1;
path[0] = 0;
tsp(1, 0, 0);
printf("Minimum cost: %d\n", min_cost);
return 0;
}
OUTPUT:
Enter the number of cities: 4
Enter the cost matrix:
0 10 15 20
10 0 35 25
15 35 0 30
20 25 30 0
Minimum cost: 80
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
REGISTER NO:211421244088
RESULT: