Discrete mathematics has various set manipulation in set theory. One of those set manipulation id union of two sets. Union of two given sets can be implemented through a C Program example. In this example, you will learn about how to perform a union of two given sets using C language

## What is the Union of Sets?

Union os two given set is a set that contains all the elements of set A and set B but only once. So, they have every element of set A and B in sorted order. This theory can be understood with an example.

Set A = {1,2,3,4,5}

Set B = {4,5,6,7}

Set A (union) Set B = {1,2,3,4,5,6,7}

## C Program to find Union of two sets

```
#include<stdio.h>
void main()
{
int a[20],b[20],c[40],i,j,m,n,flag=0,k=0,p;
printf("Enter no.of elements in to the first set: ");
scanf("%d",&m);
printf("Enter %d numbers : ",m);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("Enter no.of elements in to the second set: ");
scanf("%d",&n);
printf("Enter %d numbers: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&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;
}
}
if(flag==0)
{
c[k]=b[i];
k++;
}
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(c[j]<c[i])
{
p=c[i];
c[i]=c[j];
c[j]=p;
}
}
}
for(i=0;i<k-1;i++)
{
printf("%d ",c[i]);
}
for(i=k-1;i<k;i++)
{
printf("%d ",c[i]);
}
printf("\n");
}
```

## Leave a Reply