# Cartesian Product of Two Sets

Let A and B be two sets, Cartesian product**A × B** is the set of all ordered pair of elements from A and B

**A × B = {{x, y} : x ∈ A, y ∈ B}**

Let A = {a, b, c} and B = {d, e, f}

The Cartesian product of two sets is

A x B = {a, d}, {a, e}, {a, f}, {b, d}, {b, e}, {b, f}, {c, d}, {c, e}, {c, f}}A has 3 elements and B also has 3 elements. The Cartesian Product has 3 x 3 = 9 elements.

In general, if there are m elements in set A and n elements in B, the number of elements in the Cartesian Product is

m x n

Given two finite non-empty sets, write a program to print Cartesian Product.

**Examples :**

Input : A = {1, 2}, B = {3, 4} Output : A × B = {{1, 3}, {1, 4}, {2, 3}, {2, 4}} Input : A = {1, 2, 3} B = {4, 5, 6} Output : A × B = {{1, 4}, {1, 5}, {1, 6}, {2, 4}, {2, 5}, {2, 6}, {3, 4}, {3, 5}, {3, 6}}

## CPP

`// C++ Program to find the Cartesian Product of Two Sets ` `#include <stdio.h> ` ` ` `void` `findCart(` `int` `arr1[], ` `int` `arr2[], ` `int` `n, ` `int` `n1) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `for` `(` `int` `j = 0; j < n1; j++) ` ` ` `printf` `(` `"{%d, %d}, "` `, arr1[i], arr2[j]); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `arr1[] = { 1, 2, 3 }; ` `// first set ` ` ` `int` `arr2[] = { 4, 5, 6 }; ` `// second set ` ` ` `int` `n1 = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `int` `n2 = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` `findCart(arr1, arr2, n1, n2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the ` `// Cartesian Product of Two Sets ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `void` `findCart(` `int` `arr1[], ` `int` `arr2[], ` ` ` `int` `n, ` `int` `n1) ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `for` `(` `int` `j = ` `0` `; j < n1; j++) ` ` ` `System.out.print(` `"{"` `+ arr1[i]+` `", "` ` ` `+ arr2[j]+` `"}, "` `); ` ` ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `// first set ` ` ` `int` `arr1[] = { ` `1` `, ` `2` `, ` `3` `}; ` ` ` ` ` `// second set ` ` ` `int` `arr2[] = { ` `4` `, ` `5` `, ` `6` `}; ` ` ` ` ` `int` `n1 = arr1.length; ` ` ` `int` `n2 = arr2.length; ` ` ` `findCart(arr1, arr2, n1, n2); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find the ` `# Cartesian Product of Two Sets ` ` ` `def` `findCart(arr1, arr2, n, n1): ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `,n): ` ` ` `for` `j ` `in` `range` `(` `0` `,n1): ` ` ` `print` `(` `"{"` `,arr1[i],` `", "` `,arr2[j],` `"}, "` `,sep` `=` `"` `",end="` `") ` ` ` `# Driver code ` `arr1 ` `=` `[ ` `1` `, ` `2` `, ` `3` `] ` `# first set ` `arr2 ` `=` `[ ` `4` `, ` `5` `, ` `6` `] ` `# second set ` ` ` `n1 ` `=` `len` `(arr1) ` `# sizeof(arr1[0]) ` `n2 ` `=` `len` `(arr2) ` `# sizeof(arr2[0]); ` ` ` `findCart(arr1, arr2, n1, n2); ` ` ` `# This code is contributed ` `# by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the ` `// Cartesian Product of Two Sets ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `void` `findCart(` `int` `[]arr1, ` `int` `[]arr2, ` ` ` `int` `n, ` `int` `n1) ` ` ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `for` `(` `int` `j = 0; j < n1; j++) ` ` ` `Console.Write(` `"{"` `+ arr1[i] + ` `", "` ` ` `+ arr2[j] + ` `"}, "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () { ` ` ` ` ` `// first set ` ` ` `int` `[]arr1 = { 1, 2, 3 }; ` ` ` ` ` `// second set ` ` ` `int` `[]arr2 = { 4, 5, 6 }; ` ` ` ` ` `int` `n1 = arr1.Length; ` ` ` `int` `n2 = arr2.Length; ` ` ` ` ` `findCart(arr1, arr2, n1, n2); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the ` `// Cartesian Product of Two Sets ` ` ` `function` `findCart(` `$arr1` `, ` `$arr2` `, ` `$n` `, ` `$n1` `) ` `{ ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `for` `( ` `$j` `= 0; ` `$j` `< ` `$n1` `; ` `$j` `++) ` ` ` `echo` `"{"` `, ` `$arr1` `[` `$i` `] ,` `" , "` `, ` ` ` `$arr2` `[` `$j` `], ` `"}"` `,` `","` `; ` `} ` ` ` `// Driver Code ` ` ` `// first set ` `$arr1` `= ` `array` `( 1, 2, 3 ); ` ` ` `// second set ` `$arr2` `= ` `array` `( 4, 5, 6 ); ` `$n1` `= sizeof(` `$arr1` `) ; ` `$n2` `= sizeof(` `$arr2` `); ` `findCart(` `$arr1` `, ` `$arr2` `, ` `$n1` `, ` `$n2` `); ` ` ` `// This code is contributed by m_kit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

{{1, 4}, {1, 5}, {1, 6}, {2, 4}, {2, 5}, {2, 6}, {3, 4}, {3, 5}, {3, 6}}

**Practical Examples:**

1) A set of playing cards is Cartesian product of a four element set to a set of 13 elements.

2) A two dimensional coordinate system is a Cartesian product of two sets of real numbers.

**Reference:**

https://en.wikipedia.org/wiki/Cartesian_product

## Recommended Posts:

- Program to determine the quadrant of the cartesian plane
- Number of possible Triangles in a Cartesian coordinate system
- Program for dot product and cross product of two vectors
- Minimum number of sets with numbers less than Y
- Ways to select one or more pairs from two different sets
- Counting sets of 1s and 0s in a binary matrix
- Count sub-sets that satisfy the given condition
- Possible two sets from first N natural numbers difference of sums as D
- Partition first N natural number into two sets such that their sum is not coprime
- First N natural can be divided into two sets with given difference and co-prime sums
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Count of sub-sets of size n with total element sum divisible by 3
- Find the Jaccard Index and Jaccard Distance between the two given sets
- Product of first N factorials
- Sum of product of x and y such that floor(n/x) = y

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.