New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Cyclic Number Square (12345, 23451, 34512)

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
  1. Cyclic Number Square output
  2. How the cyclic number square works
  3. Cyclic Number Square in C
  4. Cyclic Number Square in C++
  5. Cyclic Number Square in Java
  6. Cyclic Number Square in Python
  7. Time and space complexity
  8. Frequently asked questions

Cyclic Number Square output

Output (n = 5):

12345
23451
34512
45123
51234
Cyclic Number Square layout diagram, row by row (n = 5)
How each row of the cyclic number square is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print n numbers (value (i + j − 2) % n + 1).
  3. 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 after n.
  • 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 21 of 24 in Number Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.