DM LAB Programs
DM LAB Programs
LIST OF EXPERIEMENTS
1. WRITE A PROGRAM IN C TO DISPLAY THE BOOLEAN TRUTH TABLE FOR
AND, OR , NOT
Program:
#include<stdio.h>
int main()
{
int a,b,i;
printf("\t\tTruth Table\n\n");
printf(" A\tB\tA.B\tA+B\t!A\t!B\n");
for(i = 0;i <= 3;i++)
{
a = i / 2;
b = i % 2;
printf(" %d\t%d\t%d\t%d\t%d\t%d\n", a, b, a&&b, a||b, !a, !b);
}
return 0;
}
Output:
Truth Table
A B A.B A+B !A !B
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0
Program:
#include<stdio.h>
int main()
{
int a[10],b[10],n1,n2;
printf("Enter size of set A\n");
scanf("%d",&n1);
printf("Enter element of set A\n");
for(int i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B\n");
scanf("%d",&n2);
printf("Enter element of set B\n");
for(int i=0;i<n2;i++)
scanf("%d",&b[i]);
Output:
Enter size of set A
2
Enter element of set A
12
Enter size of set B
4
Enter element of set B
4567
{ (1 4) (1 5) (1 6) (1 7) (2 4) (2 5) (2 6) (2 7)}
Program:
#include <stdio.h>
void main()
{
int i,j,k,p,ch,n1,n2,set1[10],set2[10], set3[20],flag;
char wish;
do
{
printf("press 1 for union");
printf("\n press 2 for intersection");
printf("\n press 3 for subtraction");
printf("\n enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the size of set1 \n");
scanf("%d",&n1);
printf("enter the element of set1\n");
for(i=0;i<n1;i++)
scanf("%d",&set1[i]);
printf("enter the size of set2\n");
scanf("%d",&n2);
printf("enter the elements of set2\n");
for(i=0;i<n2;i++)
scanf("%d",&set2[i]);
k=0;
for(i=0;i<n1;i++)
{
set3[k]=set1[i];
k++;
}
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(set2[i]==set1[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=set2[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf( "%d",set3[k]);
}
break;
case 2:
printf("Intersection");
printf("enter the size of sets1");
scanf("%d",&n1);
printf("enter the element of set1");
for(i=0;i<n1;i++)
scanf("%d",&set1[i]);
printf("enter the size of sets2");
scanf("%d",&n2);
printf("enter the elements of set2");
for(i=0;i<n2;i++)
scanf("%d",&set2[i]);
k=0;
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(set2[i]==set1[j])
{
flag=0;
break;
}
}
if(flag==0)
{
set3[k]=set2[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf( "%d",set3[k]);
}
break;
case 3:
printf("Set Difference");
printf("enter the size of sets1");
scanf("%d",&n1);
printf("enter the element of set1");
for(i=0;i<n1;i++)
scanf("%d",&set1[i]);
printf("enter the size of sets2");
scanf("%d",&n2);
printf("enter the elements of set2");
for(i=0;i<n2;i++)
scanf("%d",&set2[i]);
k=0;
for(i=0;i<n1;i++)
{
flag=1;
for(j=0;j<n2;j++)
{
if(set1[i]==set2[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=set1[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf( "%d",set3[k]);
}
break;
default: printf("completed");
}
}while(ch<=3);
}
Output:
press 1 for union
press 2 for intersection
press 3 for subtraction
enter ur choice 1
enter the size of set1
4
enter the element of set1
5614
enter the size of set2
3
enter the elements of set2
237
Union is
1234567
Program:
#include <stdio.h>
if(num>0)
{
count++;
countDigits(num/10);
}
else
{
return count;
}
}
int main()
{
int number;
int count=0;
count=countDigits(number);
return 0;
}
Output:
Enter a positive integer number: 12345
Total digits in number 12345 is: 5
Program:
#include<stdio.h>
int main()
{
int cost[10][10],visited[10]={0},i,j,n,no_e=1,min,a,b,min_cost=0;
printf("Enter number of nodes ");
scanf("%d",&n);
printf("Enter cost in form of adjacency matrix\n");
//input graph
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
// cost is 0 then initialize it by maximum value
if(cost[i][j]==0)
cost[i][j]=1000;
}
}
Output:
Enter number of nodes 6
Enter cost in form of adjacency matrix
044000
402000
420324
003003
002003
004330
1 t0 o 2 cost=4
2 to 3 cost=2
3 to 5 cost=2
3 to 4 cost=3
4 to 6 cost=3
minimum weight is 14
Output:
Vertex Distance from Source
00
12
25
31
46
55