Let us understand what is a Latin square matrix and then let us implement Latin square in C programming using functions and arrays.
What is a Latin Square Matrix?
A Latin square is basically a multi-dimensional array which is applied in combinatorics. The Latin square matrix has different symbols which occur only once in a row and a column.
A Latin square can be regarded as a reduced Latin square if the first row and the first column are in their natural order of occurrence as shown in the example below.
If the Latin square array is not in the reduced form, then it can be converted to a reduced form by transforming the rows and columns which is also known as permutations.
For larger squares, Matthew’s algorithm and Jacobson algorithm are used for displaying it.
Must Read: C Program To Print Pascal Triangle
Latin Square Example

Note: This code to display Latin square in C programming has been compiled with CodeLite IDE with GNU GCC compiler in Microsoft Windows 10 operating system.
Method 1: C Program To Generate Latin Square using Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include<stdio.h> void latin_square_function(int limit); int main() { int limit; printf("Enter the limit:\t"); scanf("%d", &limit); printf("\nLatin Square Matrix\n\n"); latin_square_function(limit); return 0; } void latin_square_function(int limit) { int temp, m = 1, n = 1, z = limit + 1; while(m <= limit) { temp = z; while(temp <= limit) { printf("%d ", temp); temp++; } for(n = 1; n < z; n++) { printf("%d ", n); } z--; printf("\n"); m++; } } |
Must Read: C Program To Print Floyd Triangle
Method 2: C Program To Print Latin Square using Arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h> int main() { int row, column, limit; int arr[20][20]; printf("Enter the limit:\t"); scanf("%d",&limit); for(row = 0; row < limit; row++) { for(column = 0; column < limit; column++) { arr[row][column] = (row + column + limit - 1) % limit; } } printf("\nLatin Square Matrix\n\n"); for(row = 0; row < limit; row++) { printf("\n"); for(column = 0; column < limit; column++) { printf("%d ", arr[row][column]); } } return 0; } |
Must Read: C Program To Implement Trapezoidal Rule
Output

Let’s discuss more on this Latin square in C programming in the comment section below. Also, let us know if you have any compilation errors and any doubts about the same.
To find more about Latin Square, check Wikipedia.