How to find out union of two set A and B using C programming?

Shashi Shekhar Azad
1 min readFeb 14, 2021

--

Algorithm:

  1. Read element of set A and B.

2. Initialize union U array as empty.

3. Copy all elements of first array A to U.

4. Do following for every element x of second array B:

4.1 If x is not present in first array A, then copy x to U.

5. Return U.

Source Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, k=0, A[10], B[10], U[25], j, n, m, f=0;
printf(“\n How many elements in SET A:”);
scanf(“%d”, &n);
printf(“\n Enter SET A elements”);
for(i=0;i<n; i++)
{
scanf(“%d”, &A[i]);
}
printf(“\n How many elements in set B: ”);
scanf(“%d”, &m);
printf(“\n Enter SET B elements: ”);
for(i=0; i<m; i++)
{
scanf(“%d”, &B[i]);
}
for(i=0; i<n; i++)
{
U[k]=A[i];
k++;
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(B[i]==A[j])
break;
}
if(j==n)
{
U[k]=B[i];
k++;
}
}
printf(“\n The union of set A and B is:{“);
for(i=0; i<k; i++)
printf(“%d”, U[i]);
printf(“}\n”);
}

Output:

Output of union operation.

--

--

Shashi Shekhar Azad
Shashi Shekhar Azad

Written by Shashi Shekhar Azad

I write words, write codes! SWE @ Dell Technologies. Master @ NIT Jalandhar.

No responses yet