0% found this document useful (0 votes)
8 views20 pages

DSL Lab KCS353

The document contains multiple C programs that demonstrate various set operations, including union, intersection, difference, power set, and Cartesian product. It also includes programs for generating Boolean truth tables, finding minimum cost spanning trees, and checking properties of relations such as reflexivity, symmetry, and transitivity. Each program is accompanied by example inputs and outputs for clarity.

Uploaded by

youban23y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views20 pages

DSL Lab KCS353

The document contains multiple C programs that demonstrate various set operations, including union, intersection, difference, power set, and Cartesian product. It also includes programs for generating Boolean truth tables, finding minimum cost spanning trees, and checking properties of relations such as reflexivity, symmetry, and transitivity. Each program is accompanied by example inputs and outputs for clarity.

Uploaded by

youban23y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

E1. Program in C for Set Union operation.

#include<stdio.h>

#include<conio.h>

int main()

int a[10],b[10],m,n,i,j;

int c[20],k=0,flag=0;

int ch;

printf("Enter the number of elements in first set:\n");

scanf("%d",&m);

printf("Enter the elements:\n");

for(i=0;i<m;i++)

scanf("%d",&a[i]);

printf("\nElement of First set:\n");

for(i=0;i<m;i++)

printf("%d\t",a[i]);

printf("\nEnter the number of elements in second set:\n");

scanf("%d",&n);

printf("Enter the elements:\n");

Name: Harsh Kumar Sharma


Roll no: 2101610130022
for(i=0;i<n;i++)

scanf("%d",&b[i]);

printf("\nElement of Second set:\n");

for(i=0;i<n;i++)

printf("%d\t",b[i]);

for(i=0;i<m;i++)

c[k]=a[i];

k++;

for(i=0;i<n;i++)

flag=0;

for(j=0;j<m;j++)

if(b[i]==c[j])

flag=1;

break;

Name: Harsh Kumar Sharma


Roll no: 2101610130022
}

if(flag==0)

c[k]=b[i];

k++;

printf("\nElement of resultant set\n");

for(i=0;i<k;i++)

printf("%d\t",c[i]);

OUTPUT
Enter the number of elements in first set:
5
Enter the elements:
1
2
3
4
5
Element of First set:
1 2 3 4 5
Enter the number of elements in second set:
5
Enter the elements:
6
7
8
5
4
Element of Second set:
6 7 8 5 4
Element of resultant set
1 2 3 4 5 6 7 8

Name: Harsh Kumar Sharma


Roll no: 2101610130022
E2. Program in C for Set Intersection operation.

#include <stdio.h>
int main()
{
int a[10], b[10], flag = 0, n1, n2, i, j;
printf("Enter set1 size : ");
scanf("%d",&n1);
printf("\nEnter set2 size : ");
scanf("%d",&n2);
printf("\nEnter set1 element : ");
for(i = 0;i < n1;i++)
scanf("%d",&a[i]);
printf("\nEnter set2 element : ");
for(i = 0;i < n2;i++)
scanf("%d",&b[i]);
printf("Intersection: ");
for(i = 0;i < n1;i++)
{
for(j = 0;j < n2;j++)
{
if(b[i] == a[j])
{
flag = 1;
}
}
if(flag == 1)
{
printf("%d ", b[i]);
}
flag = 0;

Name: Harsh Kumar Sharma


Roll no: 2101610130022
}
return 0;
}

OUTPUT

Enter set1 size : 5


Enter set2 size : 3
Enter set1 element : 1 2 3 4 5
Enter set2 element : 1 2 6
Intersection: 1, 2

Name: Harsh Kumar Sharma


Roll no: 2101610130022
E3. Program in C for Set Difference operation.

#include<stdio.h>
#define max 100

int ifexists(int z[], int u, int v)


{
int i;
if (u==0) return 0;
for (i=0; i<=u;i++)
if (z[i]==v) return (1);
return (0);
}

void main()
{
int p[max], q[max], r[max];
int m,n;
int i,j,k;
printf("Enter length of first array:");
scanf("%d",&m);
printf("Enter %d elements of first array\n",m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
printf("\nEnter length of second array:");
scanf("%d",&n);
printf("Enter %d elements of second array\n",n);
for(i=0;i<n;i++ ) scanf("%d",&q[i]);
k=0;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if (p[i]==q[j])
{
break;
}
}
if(j==n)
{
if(!ifexists(r,k,p[i]))
{
r[k]=p[i];
k++;
}

Name: Harsh Kumar Sharma


Roll no: 2101610130022
}
}
printf("\nThe difference of the two array is:\n");
for(i = 0;i<k;i++)
printf("%d\n",r[i]);
}
OUTPUT

Enter length of first array:4


Enter 4 elements of first array
1
2
3
4
Enter length of second array:4
Enter 4 elements of second array
2
4
5
6
The difference of the two array is:
1
3

Name: Harsh Kumar Sharma


Roll no: 2101610130022
E4. Program in C for Power set operation on a set.

#include <stdio.h>
#include <math.h>

voidprintPowerSet(char*set, intset_size)
{
/*set_size of power set of a set with set_size
n is (2**n -1)*/
unsigned intpow_set_size = pow(2, set_size);
intcounter, j;
/*Run from counter 000..0 to 111..1*/
for(counter = 0; counter < pow_set_size; counter++)
{
for(j = 0; j < set_size; j++)
{
/* Check if jth bit in the counter is set
If set then pront jth element from set */
if(counter & (1<<j))
printf("%c", set[j]);
}
printf("n");
}
}
/*Driver program to test printPowerSet*/
intmain()
{
charset[] = {'a','b','c'};
printPowerSet(set, 3);
getchar();
return0;}

Name: Harsh Kumar Sharma


Roll no: 2101610130022
OUTPUT
a
b
ab
c
ac
bc
abc

Name: Harsh Kumar Sharma


Roll no: 2101610130022
E5. Program in C for Boolean truth table for AND, OR, NOT.

#include<stdio.h>
void main()
{
int a[2][2],b[2][2],c[2];
int i,j;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
a[i][j]=(i&&j);
b[i][j]=(i||j);
}
}

for(i=0;i<=1;i++)
{
c[i]=(!i);
}

printf("\nThe Truth Table for AND Gate( && ) is..\n");


printf(" A B : C=A&&B\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf(" %d %d : %d\n",i,j,a[i][j]);
}
}
printf("\nThe Truth Table for OR Gate( || ) is..\n");
printf(" A B : C=A||B\n");

Name: Harsh Kumar Sharma


Roll no: 2101610130022
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf(" %d %d : %d\n",i,j,b[i][j]);
}
}
printf("\nThe Truth Table for NOT Gate (!) is..\n");
printf(" A : B = !A\n");
for(i=0;i<=1;i++)
{
printf(" %d : %d\n",i,c[i]);
}
}

OUTPUT

The Truth Table for AND Gate( && ) is..

A B : C=A&&B

0 0 : 0

0 1 : 0

1 0 : 0

1 1 : 1

Name: Harsh Kumar Sharma


Roll no: 2101610130022
The Truth Table for OR Gate( || ) is..

A B : C=A||B

0 0 : 0

0 1 : 1

1 0 : 1

1 1 : 1

The Truth Table for NOT Gate (!) is..

A : B = !A

0 : 1

1 : 0

Name: Harsh Kumar Sharma


Roll no: 2101610130022
E6. C prog to find Cartesian product of two sets.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],b[50],c[50],i,s1,s2,j,k;
printf("Enter how many elements in set 1\n");
scanf("%d",&s1);
printf("Enter how many elements in set 2\n");
scanf("%d",&s2);
printf("Enter elements of set 1\n");
for(i=0;i<s1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter elements of set 2\n");
for(i=0;i<s2;i++)
{
scanf("%d",&b[i]);
}
printf("cartessian product=");
printf("{");
for(i=0;i<s1;i++)
{
for(j=0;j<s2;j++)
{
printf("(%d,%d)",a[i],b[j]);
printf(",");
}
}
printf("}");}

Name: Harsh Kumar Sharma


Roll no: 2101610130022
OUTPUT
Enter how many elements in set 1
6
Enter how many elements in set 2
5
Enter elements of set 1
1
2
3
4
5
6
Enter elements of set 2
6
3
2
5
8
cartessian
product={(1,6),(1,3),(1,2),(1,5),(1,8),(2,6),(2,3),(2,2),(2,5),(2,8),(3,6),(3,3),(3,2),(3,5),(3,8),(4,6),
(4,3),(4,2),(4,5),(4,8),(5,6),(5,3),(5,2),(5,5),(5,8),(6,6),(6,3),(6,2),(6,5),(6,8),}

Name: Harsh Kumar Sharma


Roll no: 2101610130022
E7. Minimum cost spanning tree
#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;
}
}

// logic for finding minimum cost spanning tree


visited[1]=1; // visited first node
while(no_e<n)
{
min=1000;
// in each cycle find minimum cost
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0)
{
min=cost[i][j];
a=i;
b=j;
}
}
}
}
//if node is not visited
if(visited[b]==0)
{
printf("\n%d to %d cost=%d",a,b,min);
min_cost=min_cost+min;
no_e++;
}
visited[b]=1;
// initialize with maximum value you can also use any other value
cost[a][b]=cost[b][a]=1000;
}

Name: Harsh Kumar Sharma


Roll no: 2101610130022
printf("\nminimum weight is %d",min_cost);
return 0;
}

OUTPUT
Enter number of nodes 6
Enter cost in form of adjacency matrix
0 4 4 0 0 0
4 0 2 0 0 0
4 2 0 3 2 4
0 0 3 0 0 3
0 0 2 0 0 3
0 0 4 3 3 0

1 to 2 cost=4
2 to 3 cost=2

Name: Harsh Kumar Sharma


Roll no: 2101610130022
E8. Reflexive, symmetric and transitive relation.

#include<stdio.h>
#include<conio.h>
void main(){
int s[20],r[20][2],i,j,k,n,m,f=0,g=0,x,y,z=99;
clrscr();
printf("enter the no. of elements in a set:");
scanf("%d",&n);
printf("enter the elements of set:");
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
printf("enter the no. of relation set:");
scanf("%d",&m);
printf("enter the pairs of relation set:");
for(i=1;i<=m;i++)
scanf("%d%d",&r[i][1],&r[i][2]);
printf("The entered relation is:");
printf("{");
for(i=1;i<=m;i++)
printf("(%d,%d),",r[i][1],r[i][2]);
printf("\b}");
//to check currect relation set elements look below
for(i=1;i<=m;i++){ //m is no of relation elemets
for(j=1;j<=n;j++){ //n is no of set elemets
if(r[i][1]==s[j])
f++;
if(r[i][2]==s[j]) // r[80][2] is metrix to store relation set elements
g++;

Name: Harsh Kumar Sharma


Roll no: 2101610130022
} //end of for loop of j
if(f==0||g==0){
printf("\nYou entered wrong relation set.");
getch();
exit(0);
}
f=0;
g=0;
} //end of for loop of i
//intially f=0 and g=0;
//to check either it is refelexive or not
f=0;
for(i=1;i<=m;i++){
if(r[i][1]==r[i][2])
f++;
}
if(f==n)
printf("\nThe enter set relation is refelexive.");
//to check either it is symmetric or not
f=0;g=0;
for(i=1;i<=m;i++){
x=r[i][1];
y=r[i][2];
g++;
for(j=1;j<=m;j++){
if(x==r[j][2]&&y==r[j][1])
f++;
}
if(f==0)
goto d;
f=0;

Name: Harsh Kumar Sharma


Roll no: 2101610130022
}
if(g==m)
printf("\nThe relation is symmetric.");
//to check either it is transitive or not
d:
f=0;g=0;
for(i=1;i<=m;i++){
for(j=1;j<=m;j++){
for(k=1;k<=m;k++){
x=r[i][1];y=r[i][2];
if(y==r[j][1])
z=r[j][2];
if(x==r[k][1]&&z==r[k][2])
f++;
}}
if(f==0)
goto h;
f=0;
}
printf("\nThe relation is transitive.");
h:
getch();
}

Name: Harsh Kumar Sharma


Roll no: 2101610130022
E9. Recursive function (sum of natural number)

#include <stdio.h>
int sum(int n);

int main() {
int number, result;

printf("Enter a positive integer: ");


scanf("%d", &number);

result = sum(number);

printf("sum = %d", result);


return 0;
}

int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}

OUTPUT

Enter a positive integer: 20

Sum = 210

Name: Harsh Kumar Sharma


Roll no: 2101610130022

You might also like