Find the union and intersection of two sets of integer store it in two array C Program

How to write a program to find the union and intersection of two sets of integer store it in two array in C Programming Language ?



Solution:
/*write a program to find the union and intersection of two sets of integer store it in two array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,b[10],k=0,inter[10],u[10];
int j,m;
clrscr();
printf("\nenter how many number :");
scanf("%d",&n);
printf("\nenter the number;");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nenter the number;");
for(i=0;i<n;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<n;j++)
{
if(b[i]==a[j])
break;
}
if(j==n)
{
u[k]=b[i];
k++;
}
}
m=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
inter[m]=a[i];
m++;
}
}
}
printf("\nthe union is :");
for(i=0;i<k;i++)
{
printf("%d,",u[i]);
}
printf("\nthe intersection is :");

for(i=0;i<m;i++)
{
printf("%d,",inter[i]);
}

getch();
}
/*
enter how many number :4

enter the number;5
8
7
1

enter the number;9
6
5
4

the union is :5,8,7,1,9,6,4,
the intersection is :5,
 */


Learn More :