C program to find union of two sets.

 what is union of two set:-

                                         if  A={1,1,2,3} and B={5,6,7,8} are two sets then union of the set 
A and B is :-

                   A ∪ B ={1,2,3,5,6,7,8}

 Program:-

 Here A and B is the two sets and C is the union of set A and B.


#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 An”);
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 Bn”);
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]);
}

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:-
1 2 3 4 5



Leave a Reply

Your email address will not be published.