# Latin Square

A Latin Square is a n x n grid filled by n distinct numbers each appearing exactly once in each row and column. Given an input n, we have to print a n x n matrix consisting of numbers from 1 to n each appearing exactly once in each row and each column.

Examples :

```Input: 3
Output:  1 2 3
3 1 2
2 3 1

Input: 5
Output:  1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Did you find any pattern in which the number are stored in a Latin Square?

• In the first row, the numbers are stored from 1 to n serially.
• the second row, the numbers are shifted to the right by one column. i.e, 1 is stored at 2nd column now and so on.
• In the third row, the numbers are shifted to the right by two columns. i.e, 1 is stored at 3rd column now and so on.
• We continue same way for remaining rows.

Note: There may be more than one possible configuration of a n x n latin square.

## C++

 `// C++ program to pritn Latin Square ` `#include ` ` `  `// Function to print n x n Latin Square ` `void` `printLatin(``int` `n) ` `{ ` `    ``// A variable to control the rotation ` `    ``// point. ` `    ``int` `k = n+1; ` ` `  `    ``// Loop to print rows ` `    ``for` `(``int` `i=1; i<=n; i++) ` `    ``{ ` `        ``// This loops runs only after first ` `        ``// iteration of outer loop. It prints ` `        ``// numbers from n to k ` `        ``int` `temp = k; ` `        ``while` `(temp <= n) ` `        ``{ ` `            ``printf``(``"%d "``, temp); ` `            ``temp++; ` `        ``} ` ` `  `        ``// This loop prints numbers from 1 to k-1. ` `        ``for` `(``int` `j=1; j

## Java

 `// Java program to pritn Latin Square ` `class` `GFG { ` `     `  `    ``// Function to print n x n Latin Square ` `    ``static` `void` `printLatin(``int` `n) ` `    ``{ ` `         `  `        ``// A variable to control the  ` `        ``// rotation point. ` `        ``int` `k = n+``1``; ` `     `  `        ``// Loop to print rows ` `        ``for` `(``int` `i = ``1``; i <= n; i++) ` `        ``{ ` ` `  `            ``// This loops runs only after ` `            ``// first iteration of outer  ` `            ``// loop. It prints ` `            ``// numbers from n to k ` `            ``int` `temp = k; ` ` `  `            ``while` `(temp <= n) ` `            ``{ ` `                ``System.out.print(temp + ``" "``); ` `                ``temp++; ` `            ``} ` `     `  `            ``// This loop prints numbers from ` `            ``// 1 to k-1. ` `            ``for` `(``int` `j = ``1``; j < k; j++) ` `                ``System.out.print(j + ``" "``); ` `     `  `            ``k--; ` `            ``System.out.println(); ` `        ``} ` `    ``}  ` `         `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `n = ``5``; ` `         `  `        ``// Invoking printLatin function ` `        ``printLatin(n); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python 3

 `# Python 3 program to print Latin Square  ` ` `  `# Function to prn x n Latin Square  ` `def` `printLatin(n):  ` ` `  `    ``# A variable to control the  ` `    ``# rotation point.  ` `    ``k ``=` `n ``+` `1` ` `  `    ``# Loop to prrows  ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``, ``1``):  ` `     `  `        ``# This loops runs only after first  ` `        ``# iteration of outer loop. It prints  ` `        ``# numbers from n to k  ` `        ``temp ``=` `k  ` `        ``while` `(temp <``=` `n) : ` `            ``print``(temp, end ``=` `" "``) ` `            ``temp ``+``=` `1` `         `  `        ``# This loop prints numbers ` `        ``# from 1 to k-1.  ` `        ``for` `j ``in` `range``(``1``, k): ` `            ``print``(j, end ``=` `" "``)  ` ` `  `        ``k ``-``=` `1` `        ``print``()  ` ` `  `# Driver Code ` `n ``=` `5` ` `  `# Invoking printLatin function  ` `printLatin(n)  ` ` `  `# This code is contributed by R_Raj `

## C#

 `// C# program to print Latin Square ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to print n x n ` `    ``// Latin Square ` `    ``static` `void` `printLatin(``int` `n) ` `    ``{ ` `         `  `        ``// A variable to control the  ` `        ``// rotation point. ` `        ``int` `k = n + 1; ` `     `  `        ``// Loop to print rows ` `        ``for` `(``int` `i = 1; i <= n; i++) ` `        ``{ ` ` `  `            ``// This loops runs only after ` `            ``// first iteration of outer  ` `            ``// loop. It prints numbers ` `            ``// from n to k ` `            ``int` `temp = k; ` ` `  `            ``while` `(temp <= n) ` `            ``{ ` `                ``Console.Write(temp + ``" "``); ` `                ``temp++; ` `            ``} ` `     `  `            ``// This loop prints numbers from ` `            ``// 1 to k-1. ` `            ``for` `(``int` `j = 1; j < k; j++) ` `                ``Console.Write(j + ``" "``); ` `     `  `            ``k--; ` `            ``Console.WriteLine(); ` `        ``} ` `    ``}  ` `         `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `n = 5; ` `         `  `        ``// Invoking printLatin function ` `        ``printLatin(n); ` `    ``} ` `} ` ` `  `// This code is contributed by KRV. `

## PHP

 ` `

Output :

```1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
```

This article is contributed by Pratik Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : KRV, vt_m, R_Raj

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.