Practical File
Of
Discrete Structure and Logic Lab
(KCS-353)
(2021-2022)
Submitted to: Submitted by:
Mr. Ankit Gupta Name- Ravi kr. Sharma
Roll No.- 2001500100086
Year/Branch- 2nd year/CSE
Index
S. Name of the Practical Page Signature
No. No.
1 Write a program in C to create two sets and
perform the Union operation on sets.
3-6
Write a program in C to create two sets and
2 perform the Intersection operation on sets.
7-9
Write a program in C to create two sets and
3 perform the Difference operation on sets.
10-13
Write a program in C to create two sets and
4 perform the Symmetric Difference operation.
14-19
Program 1-
Aim- Write a program in C to create two sets and perform the
Union operation on sets.
Algorithm-
Step 1: Start
Step 2: set a1 = (1,4,5,7,9) // a1 [0] =1, a1[1] =4, a1 [2] =5, a1 [3] =7, a1[4] =9 // i
set a2 = (2,3,6,8) // a2[0] =2, a2[1] =3, a2[2] =6, a2[3] =8 // j
Use two sets a1[i], a2[j] and a3[k] for union
Step 3: If a1[i]<a2[j] then print a1[i] and increment i.
Step 4: If a1[i]> a2[j] then print a2[j] and increment j.
Step 5: If a1[i] =a2[j] and increment both i and j.
Step 6: Print remaining elements of the larger set.
union of both sets = (1,2,3,4,5,6,7,8,9)
Step 7: End
Code-
#include<stdio.h>
#include<conio.h>
void main()
int a[10],b[10],i,c[10],j,k=0,n1,n2;
printf("Enter number of element of set A\n");
scanf("%d",&n1);
printf("Enter the element of set A \n");
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter number of element of set B\n");
scanf("%d",&n2);
printf("Enter the element of set B \n");
for(i=0;i<n2;i++)
scanf("%d",&b[i]);
for(i=0;i<n1;i++)
for(j=0;j<k;j++)
if(c[j]==a[i])
break;
if(j==k)
c[k]=a[i];
k++;
for(i=0;i<n2;i++)
for(j=0;j<k;j++)
if(c[j]==b[i])
break;
if(j==k)
c[k]=b[i];
k++;
}
printf("Union of set A and B is:-\n");
for(i=0;i<k;i++)
printf("%d ",c[i]);
Output-
1-
2-
3-
Program 2-
Aim- Write a program in C to create two sets and perform
the Intersection operation on sets.
Algorithm-
Step 1- Start
Step 2- a1[0] =1, a1[1] =3, a1 [2] =4, a1[3] =5, a1[4] =7
a2[0] =1, a2[1] =3, a2 [2] =5, a2[3] =6
a1[i]= {1,3,4,5,7}
a2[j]= {1,3,5,6}
Use set a1[i] and a2[j].
Step 3- If a1[i] < a2[j] then increment i.
Step 4- If a1[i] > a2[j] then increment j.
Step 5- If both are same then print any of them and increment both i and j.
Step 6- End
Code-
#include<stdio.h>
int main()
{
int a[100],b[100],c[100],n1,n2,n,k=0,i,j;
printf("Enter number of element of set A\n");
scanf("%d",&n1);
printf("Enter elements of set A\n");
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter number of element of set B\n");
scanf("%d",&n2);
printf("Enter elements of set B\n");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);
for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
{
c[k]=a[i];
k++;
}
}
}
printf("intersection of set A and set B is:-\n");
for (i=0;i<k;i++)
printf("%d ",c[i]);
return 0;
}
Output-
1-
2-
3-
Program 3-
Aim- Write a program in C to create two sets and perform
the Difference operation on sets.
Algorithm-
Step 1: Start
Step 2: Define two sets, A and B, and add elements to them
A = {2, 3, 4} and B = {4, 5, 6}
Step 3: Pick one element from array A and compare it with all the elements of
the array B.
Step 4: If the element of array A exists in array B, discard that element and
pick the next element from array A and repeat steps from 3.
Step 5: If the element of array A does not exist in array B, add that element in
array C.
Step 6: While adding that element into array C, ensure that it does not contain
in array C.
Step 7: Repeat steps 3 to 5 until all the elements of array A are compared.
Step 8: Print the elements in Array c which will represents the difference
between given two arrays
Step 9- End
Code-
#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 Set:");
scanf("%d",&m);
printf("Enter %d elements of first Set\n",m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
printf("\nEnter length of second Set:");
scanf("%d",&n);
printf("Enter %d elements of second Set\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++;
printf("\nThe difference of the two Set is:\n");
for(i = 0;i<k;i++)
printf("%d\n",r[i]);
Output-
1-
2-
3-
Program 4-
Aim- Write a program in C to create two sets and perform
the Symmetric Difference operation.
Algorithm-
Step 1: Start
Step 2: Define two sets, A and B, and add elements to them
A = {2, 3, 4} and B = {4, 5, 6}
Step 3: Pick one element from array A and compare it with all the elements of
the array B.
Step 4: If the element of array A exists in array B, discard that element and
pick the next element from array A and repeat steps from 3.
Step 5: If the element of array A does not exist in array B, add that element in
array C.
Step 6: Repeat steps 3 to 5 until all the elements of array A are compared.
Step 7: Print the elements in Array C which will represents the symmetric
difference between given two arrays.
Step 8- End
Code-
#include <stdio.h>
int main()
int a[50], b[50], c[50], fl = 0;
int i, j, n, m, k;
printf("Enter the number of elements in first set:\n");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
}
printf("\nElement of First set:\n");
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
printf("\nEnter the number of elements in second set:\n");
scanf("%d", &m);
printf("\nEnter the elements:\n");
for (i = 0; i < m; i++)
scanf("%d", &b[i]);
printf("\nElement of Second set:\n");
for (i = 0; i < m; i++)
printf("%d\t", b[i]);
k = 0;
for (i = 0; i < n; i++)
fl = 0;
for (j = 0; j < m; j++)
if (a[i] == b[j])
fl = 0;
break;
else
fl = 1;
}
if (fl == 1)
c[k] = a[i];
k++;
for (i = 0; i < m; i++)
fl = 0;
for (j = 0; j < n; j++)
if (b[i] == a[j])
fl = 0;
break;
else
fl = 1;
if (fl == 1)
c[k] = b[i];
k++;
printf("\n Symmetric Difference: ");
for (i = 0; i < k; i++)
if (c[i] != c[i + 1])
{
printf("%d\t,", c[i]);
return 0;
Output-
1-
2-
3-