Practical File of Discrete Structure and Logic Lab (KCS-353) (2021-2022)
Practical File of Discrete Structure and Logic Lab (KCS-353) (2021-2022)
Of
Discrete Structure and Logic Lab
(KCS-353)
(2021-2022)
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
Code-
#include<stdio.h>
#include<conio.h>
void main()
int a[10],b[10],i,c[10],j,k=0,n1,n2;
scanf("%d",&n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter number of element of set B\n");
scanf("%d",&n2);
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-
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}
Code-
#include<stdio.h>
int main()
{
int a[100],b[100],c[100],n1,n2,n,k=0,i,j;
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-
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>
int i;
if (u==0) return 0;
return (0);
}
void main()
int m,n;
int i,j,k;
scanf("%d",&m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
scanf("%d",&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++;
for(i = 0;i<k;i++)
printf("%d\n",r[i]);
Output-
1-
2-
3-
Program 4-
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 i, j, n, m, k;
scanf("%d", &n);
scanf("%d", &a[i]);
}
printf("\nElement of First set:\n");
printf("%d\t", a[i]);
scanf("%d", &m);
scanf("%d", &b[i]);
printf("%d\t", b[i]);
k = 0;
fl = 0;
if (a[i] == b[j])
fl = 0;
break;
else
fl = 1;
}
if (fl == 1)
c[k] = a[i];
k++;
fl = 0;
if (b[i] == a[j])
fl = 0;
break;
else
fl = 1;
if (fl == 1)
c[k] = b[i];
k++;
printf("%d\t,", c[i]);
return 0;
Output-
1-
2-
3-