When you have learned about Discrete structures you must have come across a set theory. In which we manipulate sets and their elements. One of the most popular manipulations is the intersection of two given sets. Well, this logic can be implemented through a C program. In this example, we will learn about the c program to implement the intersection of two given sets.

## What is Intersection of Sets?

The intersection of two sets is known as a set that contains only elements that are common from Set A and Set B. This theory can be explained through an example.

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

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

Set A (intersection) Set B = {3,4,5}

## C Program For Intersection of Two Sets

```
#include<stdio.h>
void main()
{
int i,j,m,n,A[50],B[50],C[50],k=0;
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++)
{
for(j=0;j<n;j++)
{
if(A[i]==B[j])
{
C[k]=A[i];
k++;
}
}
}
for(i=0;i<k-1;i++)
{
printf("%d ",C[i]);
}
for(i=k-1;i<k;i++)
printf("%d \n",C[i]);
}
```

## Leave a Reply