0% found this document useful (0 votes)
44 views

DSTL Practical Files

The document contains programs in C language to perform set operations like union, intersection, difference, symmetric difference and power set on sets. It also contains a program to display truth table for logical operations like AND, OR and NOT.

Uploaded by

gurjas singh
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)
44 views

DSTL Practical Files

The document contains programs in C language to perform set operations like union, intersection, difference, symmetric difference and power set on sets. It also contains a program to display truth table for logical operations like AND, OR and NOT.

Uploaded by

gurjas singh
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/ 14

SIR CHHOTU RAM INSTITUTE OF

ENGINEERING AND TECHNOLOGY


CCSU , MEERUT

DSTL PRACTICAL FILE

SUBMITTED TO:
ER. NEELAM SINGH

SUBMITTED BY:
NAME: SUPRIYA SHARMA
COURSE: BTECH IT 2nd YEAR
SEMESTER: 3rd SEM
ROLL NO: 100200565
CERTIFICATE

This is certified to be the bonafide work of SUPRIYA


SHARMA From BTECH IT 2nd YEAR , 3rd SEM
in the laboratory of DSTL during the academic year
2021-2022.

Teacher In-charge:………………….

Examiner:……………………




TABLE OF CONTENT
1 . Write a program in C to create two sets and perform the union
operation on sets.

2. Write a program in C to create two sets and perform the intersection
operation on sets.

3. Write a program in C to create two sets and perform the difference
operation on sets.

4. Write a program in C to create two sets and perform the symmetric
difference operation .

5. Write a program in C to perform the power set operation on a set.

6. Write a program in C to display the Boolean Truth Table for AND , OR
and NOT.


1. WRITE A PROGRAM IN C TO CREATE TWO SETS AND PERFORM
THE UNION OPERATION ON SETS.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],i,c[10],j,k=0,n1,n2;

// taking input set A

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]);

// taking input set B

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]);

// logic for calculate union

// copy the element of set A in set C


for(i=0;i<n1;i++)
{
// repeted element is not allowed so we check is any
value repeted
for(j=0;j<k;j++)
{
if(c[j]==a[i])
break;
}
if(j==k) //if not repesated then store value in set c
{
c[k]=a[i];
k++;
}
}
// copy element of set B in set C
for(i=0;i<n2;i++)
{
// check for repeted element
for(j=0;j<k;j++)
{
if(c[j]==b[i])
break;
}
if(j==k) // if element is not repeted then store in set C
{
c[k]=b[i];
k++;
}
}

// printing of union of set A and set B


printf("Union of set A and B is:-\n");
for(i=0;i<k;i++)
printf("%d ",c[i]);
}


2. WRITE A PROGRAM IN C TO CREATE TWO SETS AND PERFORM
THE INTERSECTION OPERATION ON SETS.
#include<stdio.h>
int main()
{
int a[100],b[100],c[100],n1,n2,n,k=0,i,j;

// taking input of set A

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]);

// taking input set B

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]);

// Logic for intersection

for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
{
c[k]=a[i];
k++;
}
}

}

// Printing the elements of intersection of set A and set B
printf("intersection of set A and set B is:-\n");
for(i=0;i<k;i++)
printf("%d ",c[i]);

return 0;
}
3.WRITE A PROGRAM IN C TO CREATE TWO SETS AND PERFORM
THE DIFFERENCE OPERATION ON SETS.

#include<stdio.h>
int main()
{
int a[10],b[10],c[10],d[10],m=0,k=0,n1,n2,l,i,j;
printf("Enter size of set A");
scanf("%d",&n1);
printf("Enter element of set");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B");
scanf("%d",&n2);
printf("Enter element of set");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);


// logic for find A-B

for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(b[j]==a[i])
break;
}
if(j==n2)
{
// here we check that is element already present in
the set
// if present than ignore it otherwise add to the
difference set
for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==k)
{
c[k]=a[i];
k++;
}
}

}

// logic for find B-A

for( i=0;i<n2;i++)
{
for(j=0;j<n1;j++)
{
if(b[i]==a[j])
break;
}
if(j==n1)
{
// here we check that is element already present in
the set
//if present than ignore it otherwise add to the
difference set
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}

}
printf("Difference of A-B is:-\n");
for(i=0;i<k;i++)
{
printf("%d ",c[i]);
}
printf("\n");
printf("Difference of B-A is:-\n");
for(i=0;i<m;i++)
{
printf("%d ",d[i]);
}
return 0;
}

4. WRITE A PROGRAM IN C TO CREATE TWO SETS AND PERFORM
THE SYMMETRIC DIFFERENCE OPERATION ON SETS.

#include<stdio.h>
int main()
{
int
a[10],b[10],c[10],d[10],m=0,k=0,n=0,n1,n2,l,i,j,sy[100];
printf("Enter size of set A");
scanf("%d",&n1);
printf("Enter element of set");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B");
scanf("%d",&n2);
printf("Enter element of set");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);


// logic for find A-B

for( i=0;i<n1;i++)
{
// here we check that is b[i] already present in the ans
set
// if present then ignore it otherwise add it to the ans
set
for(j=0;j<n2;j++)
{
if(b[j]==a[i])
break;
}
if(j==n2)
{

for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==k)
{
c[k]=a[i];
k++;
}
}

}

// logic for find B-A

for( i=0;i<n2;i++)
{
for(j=0;j<n1;j++)
{
if(b[i]==a[j])
break;
}
if(j==n1)
{
// here we check that is b[i] already present in the
ans set
// if present then ignore it otherwise add it to the
ans set
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}

}

//logic for symmetric Difference

for(i=0;i<k;i++)
{
sy[n]=c[i];
n++;
}
for(i=0;i<m;i++)
{
sy[n]=d[i];
n++;
}

printf("\nsymmetric Difference of sets is:-\n");
for(i=0;i<n;i++)
printf("%d ",sy[i]);
return 0;

}






5. WRITE A PROGRAM IN C TO PERFORM THE POWER SET
OPERATION ON A SET.

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

void printPowerSet(char *set, int set_size)
{
/*set_size of power set of a set with set_size
n is (2**n -1)*/
unsigned int pow_set_size = pow(2, set_size);
int counter, 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 print jth element from set */
if(counter & (1<<j))
printf("%c", set[j]);
}
printf(" ");
}
}

/*Driver program to test printPowerSet*/
int main()
{
char set[] = {'a','b','c'};
printPowerSet(set, 3);
return 0;
}

6. WRITE A PROGRAM IN C TO DISPLAY THE BOOLEAN TRUTH
TABLE FOR AND , OR , NOT.

#include<stdio.h>

// logic for OR gate



int find_OR(int x,int y)
{
if(x==1 && y==1)
return 1;
if(x==1 && y==0 || x==0 && y==1)
return 1;
if(x==0 && y==0)
return 0;
}

// logic for find AND

int find_AND(int x,int y)
{
if(x==1 && y==1)
return 1;
else
return 0;
}

// logic for find NOT

int find_NOT(int x)
{
if(x==1)
return 0;
else
return 1;
}

// Driver function

int main()
{
int ch,a,b;
printf("1. OR\n");
printf("2. AND\n");
printf("3. NOT\n");
printf("4 .exit\n");
while(1)
{
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Give two input 1 for true and 0 for
false\n");
scanf("%d%d",&a,&b);
printf("%d",find_OR(a,b));
break;
case 2: printf("Give two input 1 for true and 0 for
false\n");
scanf("%d%d",&a,&b);
printf("%d",find_AND(a,b));
break;
case 3: printf("Give an input 1 for true and 0 for
false\n");
scanf("%d",&a);
printf("%d",find_NOT(a));
break;
case 4: exit(0);
default: printf("Wrong key\n");
}
}
}

You might also like