Header Ads Widget

C program to find Cartesian product of two set.

 What is cartesian product of two set:-

                                                            if set A={1,2} and set B={4,5,6,7}  then cartesian product of set A and B is :-

          A*B={ (1,4),(1,5),(1,6),(1,7),(2,4),(2,5),(2,6),(2,7) }

Program:-

#include<stdio.h>
int main()
{
int a[10],b[10],n1,n2;
printf("Enter size of set A\n");
scanf("%d",&n1);
printf("Enter element of set A\n");
for(int i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B\n");
scanf("%d",&n2);
printf("Enter element of set B\n");
for(int i=0;i<n2;i++)
scanf("%d",&b[i]);
// logic for cartesian product
printf("{");
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
printf(" (%d %d) ",a[i],b[j]);
}
}
printf("}");
return 0;
}

Output:-


Enter size of set A
2
Enter element of set A
1 2
Enter size of set B
4
Enter element of set B
4 5 6 7
{ (1,4),(1,5),(1,6),(1,7),(2,4),(2,5),(2,6),(2,7) }



Post a Comment

1 Comments