In the cyclic number square every row is the row above shifted one place to the left, with the first number moved to the end: 12345, 23451, 34512. Each number appears exactly once in every row and every column.
Quick answer: Print n numbers on every row, and use (i + j − 2) % n + 1 as the value. The % (remainder) makes the count wrap around: after 5 it goes back to 1, so row 2 reads 2, 3, 4, 5, 1.
This program is part of the Number Patterns collection.
Table of contents8 sections
Cyclic Number Square output
Output (n = 5):
12345 23451 34512 45123 51234

Same shape, other characters: the Number Square and Number and Symbol Fill Pattern programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.
How the cyclic number square works
Think of the numbers 1 to n arranged in a circle. Row i starts reading the circle at i and goes once round. In code, i + j − 2 counts positions from 0, the remainder by n keeps that count between 0 and n − 1, and adding 1 turns it back into a number from 1 to n. A grid like this, where no number repeats in a row or column, is called a Latin square.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
nnumbers (value(i + j − 2) % n + 1). - Move to the next line.
Key takeaways
- Each row is the previous row rotated left by one.
- The
%operator wraps the count back to the start aftern. - Every number appears once in each row and each column: a Latin square.
- The anti-diagonals (from top right to bottom left) each hold a single repeated number.
Cyclic Number Square in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
printf("%d", (i + j - 2) % n + 1);
printf("\n");
}
return 0;
}Cyclic Number Square in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
cout << (i + j - 2) % n + 1;
cout << endl;
}
return 0;
}Cyclic Number Square in Java
public class CyclicNumberSquare {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
System.out.print((i + j - 2) % n + 1);
System.out.println();
}
}
}Cyclic Number Square in Python
n = 5
for i in range(1, n + 1):
for j in range(1, n + 1):
print((i + j - 2) % n + 1, end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
How do I rotate to the right instead (15234)?
Use (j − i + n) % n + 1. The extra + n keeps the value from going negative before the remainder is taken, which matters in C, C++ and Java.
Why subtract 2 inside the formula?
Both i and j start at 1. Subtracting 2 makes the first cell 0, which is what the remainder needs; the + 1 at the end moves the result back to the range 1 to n.
Where are Latin squares used?
Sudoku is a Latin square with extra rules for the boxes. They are also used to plan experiments and tournament schedules, so that every pair meets exactly once.






