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

Sample File

The document contains C program code to perform various set operations - union, intersection, difference, symmetric difference, and power set. It also contains code to display truth tables for logical AND, OR, and NOT operations, as well as code to find the Cartesian product of two sets. The programs take input sets, perform the specified operation, and output the results.

Uploaded by

reapermorning98
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 views14 pages

Sample File

The document contains C program code to perform various set operations - union, intersection, difference, symmetric difference, and power set. It also contains code to display truth tables for logical AND, OR, and NOT operations, as well as code to find the Cartesian product of two sets. The programs take input sets, perform the specified operation, and output the results.

Uploaded by

reapermorning98
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

#c program to create two sets and perform the union operation on sets.

int main(){

int a[10],b[10],c[10],i,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]);

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]);
return 0;
}
output

enter number of element of set A


3
enter the element of set A
1
1
2
enter number of element of set B
4
enter the element of set B
3
4
5
2
union of set A and B is:
12345
#c program to create two set 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 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 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<n2;j++){
if(a[i]==b[j]){
c[k]=a[i];
k++;

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

return 0;

OUTPUT

enter number of element of set A


3
enter element of set A
1
2
3
enter number of element of set B
4
enter element of set B
1
1
3
4
intersection of set A and B is:
113
#c program to create two sets and and perform the difference operation on sets

#include<stdio.h>
void sort(int size,int ab[]);
void main(){
int i,n1,n2,j=0,k=0,l,flag=0;
printf("Enter number of elements of 1st set \n");
scanf("%d",&n1);
printf("Enter size of an set2 \n");
scanf("%d",&n2);
int set1[n1],set2[n2],diff[n1+n2];
printf("Enter numbers for set 1 \n");
for(i=0;i<n1;i++){
scanf("%d",&set1[i]);
}
printf("Enter numbers for set 2 \n");
for(i=0;i<n2;i++){
scanf("%d",&set2[i]);
}
for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(set2[j]==set1[i])
break;
}
if(j==n2)
{
for(l=0;l<k;l++)
{
if(diff[l]==set1[i])
break;
}
if(l==k)
{
diff[k]=set1[i];
k++;
flag++;
}
}
}
sort(n1+n2,diff);
printf("\nDifference of sets \n");
if(flag==0){
printf("is an empty set"); }
else{
printf("{ ");
for(i=0;i<flag;i++){
printf("%d ",diff[i]);
}
printf(" } "); }
}// closing main
void sort(int size,int a[]){
int i,j,temp;
for(i=0;i<size;i++){
for(j=i+1;j<size;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}

Output

Enter number of elements of 1st set


3
Enter size of an set2

3
Enter numbers for set 1
1
2
3
Enter numbers for set 2
3
4
5

Difference of sets
{12 }
#c programing to create two sets and perform the symmetric difference
operation

#include<stdio.h>
#include<conio.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:-\n");
scanf("%d",&n1);
printf("Enter element of set:-\n");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B:-\n");
scanf("%d",&n2);
printf("Enter element of set:-\n");
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)
{
for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==0);
{
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)
{
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}
}
printf("\n set A is :-\n");
for(i=0; i<n1;i++)
{
printf("%d ", a[i]);
}
printf("\n set B is :-\n");
for(i=0; i<n2;i++)
{
printf("%d ", b[i]);
}
//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]);
getch();
}
Output

Enter size of set A:-


3
Enter element of set:-
1
2
3
Enter size of set B:-
3
Enter element of set:-
3
4
5

set A is :-
123
set B is :-
345
symmetric Difference of sets is:-
1245
#C Program To Perform The Power Set Operation On A Set

#include <stdio.h>
#include <math.h>
void printPowerSet(int *set, int set_size)
{
unsigned int pow_set_size = pow(2, set_size);
int counter, j;
for(counter = 0; counter < pow_set_size; counter++)
{
for(j = 0; j < set_size; j++)
{
if(counter & (1<<j))
printf("{%d}", set[j]);
}
printf("\n");
}
}
int main()
{
int set[] = {1,2,3,4,5,6};
Print PowerSet(set, 6);
return 0;
}

OUTPUT:

{1}
{2}
{1}{2}
{3}
{1}{3}
{2}{3}
{1}{2}{3}
{4}
{1}{4}
{2}{4}
{1}{2}{4}
{3}{4}
{1}{3}{4}

{2}{3}{4}
{1}{2}{3}{4}
#C Program To Display The Boolean Truth Table For And, OR, Not.

#include<stdio.h>
void main()
{
int a,b,temp,i;
printf("\n \tAND TRUTH TABLE \n \n \tA B OUTPUT \n");
for(i=0; i<4; i++)
{
temp=i;
a=i%2;
temp/=2;
b=temp%2;
printf("\n\t%d %d %d \n",b,a,a&&b);
}
printf("\n \tOR TRUTH TABLE \n \n \tA B OUTPUT \n");
for(i=0; i<4; i++)
{
temp=i;
a=i%2;
temp/=2;
b=temp%2;
printf("\n\t%d %d %d \n",b,a,a||b);
}
printf("\n \tNOT TRUTH TABLE \n \n \tA OUTPUT \n");
for(i=0; i<2; i++)
{
temp=i;
a=i%2;
temp/=2;
b=temp%2;
printf("\n\t%d %d \n",a,!a);
}
}
OUTPUT:

AND TRUTH TABLE

A B OUTPUT

0 0 0

0 1 0

1 0 0

1 1 1

OR TRUTH TABLE

A B OUTPUT

0 0 0

0 1 1

1 0 1

1 1 1

NOT TRUTH TABLE

A OUTPUT

0 1

1 0
#C Program To Find Cartesian Product Of Two Sets

#include<stdio.h>
int main()
{
int a[10],b[10],n1,n2,i,j,count=0;
printf("Enter size of set A:\n");
scanf("%d",&n1);
printf("Enter size of set B;\n");
scanf("%d",&n2);
if (n1==0|| n2==0) {
printf("Product is not Possible!!!\n");
}
else {
printf("Enter element of setA:\n");
for( i=0; i<n1; i++)
scanf("%d",&a[i]);
printf("Enter element of setB:\n");
for( j=0; j<n2; j++)
scanf("%d",&b[j]);
for(i=0; i<n1; i++) {
for(j=0; j<n2; j++) {
printf("(%d,%d)\n",a[i],b[j]);
}
}
count=n1*n2;
printf("The no. of combination are %d : ",count);
}
return 0;
}
OUTPUT:

Enter size of set A:


4
Enter size of set B;
4
Enter element of setA:
1
2
3
4
Enter element of setB:
3
5
6
7
(1,3)
(1,5)
(1,6)
(1,7)
(2,3)
(2,5)
(2,6)
(2,7)
(3,3)
(3,5)
(3,6)
(3,7)
(4,3)
(4,5)
(4,6)
(4,7)
The no. of combination are 16 :

You might also like