info@meritcampus.com    +91-85006-22255
...
`

Write a program to print Latin Square of given number.

Input (int) Output
3 `1 2 32 3 13 1 2`
4 `1 2 3 42 3 4 13 4 1 24 1 2 3`
5 `1 2 3 4 52 3 4 5 13 4 5 1 24 5 1 2 35 1 2 3 4`
6 `1 2 3 4 5 62 3 4 5 6 13 4 5 6 1 24 5 6 1 2 35 6 1 2 3 46 1 2 3 4 5`

``` class PrintLatinSquare {     public static void main(String s[])    {        printLatinSquare(5);    }     public static void printLatinSquare(int number) { ```
```     } } ```
Topic:

User comments below.All of them might not be correct.

*here we need to print in the form of the rows and cols so here we need to use nested loops
* take outer for loop starts from 0 and till to reach <=number
* now take another temp variable to store i value for every iteration
temp=i;
*now start inner loop starts from 0 and till to reach <=number
* now check of the temp>number than make temp=1;
* print t with space and increase the temp value
* after coming out of the inner loop print the newline

Posted by Uday Kumar    2015-02-10 07:18:41

To print Latin square of given number,nested for loop should be used.

--outer loop counter say I runs from 1 till number.

--Inner Loop counter say j runs from I till number

--inside inner loop,if condition should be there to check if j>number if yes then set j=1 and print the value of j using " " empty space between two numbers.

--Last line of outer loop should be println() method to move cursor to next line.

Posted by Mânïshå Mùlchåndânï    2015-02-10 14:43:07

here we uses nested loop
let given input is k=3
print output as
1 2 3
2 3 1
3 1 2
start outer loop from i=0 to k
and inside outerloop store i value in temp varibale
in innerloop from j=temp to k+i
and print(value=(j%k)+1)
for every itaration of outerloop go to  next line...
ex: k=3
1 to 3
1st iteration:j=temp=0 to k+0(means 3+0=3)
j%k+1=(0%3)+1=1,  j++
j%k+1=(1%3)+1=2   j++
j%k+1=(2%3)+1=3   j++
goto nextline

2nd iteration:j=temp=1 to k+1(means 3+1=4)
j%k+1=(1%3)+1=2,  j++
j%k+1=(2%3)+1=3   j++
j%k+1=(3%3)+1=1   j++
goto nextline

3rd iteration:j=temp=2 to k+2(means 3+2=5)
j%k+1=(2%3)+1=3,  j++
j%k+1=(3%3)+1=1   j++
j%k+1=(4%3)+1=2   j++
goto nextline

Posted by Goutham Singarapu    2015-02-10 17:08:08

This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-12 05:28:42