Placement Prep

Diamond Number Patterns in C, C++, Python, and Java

Three diamond number patterns: repeated-digit, star-separated, and consecutive, with working C, C++, Python, and Java code, loop traces, and placement-test context.

By FACE Prep Team 6 min read
pattern-programs number-patterns c-programming c-plus-plus python java placement-prep coding-interview

Diamond number patterns build on the same nested-loop skeleton used in triangle and pyramid problems, adding a lower-half mirror that tests whether the loop bounds are derived or just guessed.

Three variants appear in placement coding tests and practice platforms. All three share the same outer-loop structure: upper half counts i from 1 to n, lower half counts i from n-1 down to 1. The inner logic for each row differs across the three. Working through each variant makes the pattern reusable in any combination an interviewer constructs.

Pattern 1: Repeated-Digit Diamond

Row i of this pattern prints the digit i exactly i times. For n=4, the output is:

1
22
333
4444
333
22
1

Seven rows total (2*4-1 = 7). Row 4 is the widest and appears once.

Loop logic

The outer loop runs in two phases. Phase 1 (upper half): i runs from 1 to n. On each row, the inner loop runs i times and prints i. Phase 2 (lower half): i runs from n-1 down to 1. Same inner loop.

The only question candidates get wrong here is the lower-half starting point. The lower half starts at n-1, not n. Starting at n would reprint the widest row and give 2n rows instead of 2n-1.

C

#include <stdio.h>

int main() {
    int n, i, j;
    printf("Enter n: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++)
            printf("%d", i);
        printf("\n");
    }
    for (i = n - 1; i >= 1; i--) {
        for (j = 1; j <= i; j++)
            printf("%d", i);
        printf("\n");
    }
    return 0;
}

Python

n = int(input("Enter n: "))
for i in range(1, n + 1):
    print(str(i) * i)
for i in range(n - 1, 0, -1):
    print(str(i) * i)

str(i) * i converts i to a string then repeats it i times. For i=3, str(3) * 3 gives "333". The Python range() function returns empty when the start already satisfies the stop condition, so range(0, 0, -1) is safely empty; no special case is needed for the lower half when n=1.

Pattern 2: Star-Separated Repeated-Digit Diamond

Same per-row digit as Pattern 1, but digits are joined by * instead of being run together. For n=4:

1
2*2
3*3*3
4*4*4*4
3*3*3
2*2
1

The structure is identical to Pattern 1. The only change is in the inner row-building logic.

Loop logic

The inner loop prints digit i, then checks whether j < i before printing *. The last digit on each row has no trailing separator. In Python, '*'.join([str(i)] * i) expresses this in one step: build a list of i copies of the string str(i), then join them with *.

C++

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            cout << i;
            if (j < i) cout << "*";
        }
        cout << "\n";
    }
    for (int i = n - 1; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            cout << i;
            if (j < i) cout << "*";
        }
        cout << "\n";
    }
    return 0;
}

Python

n = int(input("Enter n: "))
for i in range(1, n + 1):
    print('*'.join([str(i)] * i))
for i in range(n - 1, 0, -1):
    print('*'.join([str(i)] * i))

The '*'.join(...) form is idiomatic Python for separator-joined repetition. For i=4, [str(4)] * 4 gives ['4', '4', '4', '4'], and joining produces "4*4*4*4".

Pattern 3: Consecutive-Number Diamond

Instead of repeating a single digit, this pattern assigns a unique incrementing number to every position in the grid. For n=4:

1
2*3
4*5*6
7*8*9*10
4*5*6
2*3
1

Row 1 gets number 1. Row 2 gets 2 and 3. Row 3 gets 4, 5, and 6. Row 4 gets 7 through 10.

Deriving the row-start formula

Row k (1-indexed) contains k numbers. The numbers in rows 1 through k-1 total 1 + 2 + ... + (k-1) = k*(k-1)/2. The first number on row k is therefore 1 + k*(k-1)/2.

Verification for n=4:

  • Row 1: 1 + 1*0/2 = 1 → “1”
  • Row 2: 1 + 2*1/2 = 2 → “2*3”
  • Row 3: 1 + 3*2/2 = 4 → “456”
  • Row 4: 1 + 4*3/2 = 7 → “789*10”

The lower half reuses the same row starts for rows n-1 down to 1. No second counter is needed.

Python

n = int(input("Enter n: "))

def row_start(k):
    return 1 + k * (k - 1) // 2

for i in range(1, n + 1):
    s = row_start(i)
    print('*'.join(str(s + j) for j in range(i)))
for i in range(n - 1, 0, -1):
    s = row_start(i)
    print('*'.join(str(s + j) for j in range(i)))

Java

import java.util.Scanner;

public class ConsecutiveDiamond {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] rowStart = new int[n];
        int num = 1;
        // Compute row starts and print upper half
        for (int i = 1; i <= n; i++) {
            rowStart[i - 1] = num;
            StringBuilder sb = new StringBuilder();
            for (int j = 1; j <= i; j++) {
                if (j > 1) sb.append("*");
                sb.append(num++);
            }
            System.out.println(sb);
        }
        // Print lower half using stored row starts
        for (int i = n - 1; i >= 1; i--) {
            int start = rowStart[i - 1];
            StringBuilder sb = new StringBuilder();
            for (int j = 1; j <= i; j++) {
                if (j > 1) sb.append("*");
                sb.append(start + j - 1);
            }
            System.out.println(sb);
        }
    }
}

The Java version stores each row’s starting number in rowStart[] during the upper half. The lower half reads from that array directly, avoiding a need to recompute the formula.

Common Loop Structure Across All Three Patterns

All three patterns share this outer skeleton:

Upper half:  i from 1 to n
Lower half:  i from n-1 down to 1

What changes is only the inner row-building instruction:

  • Pattern 1: print i, i times, no separator.
  • Pattern 2: print i, i times, with * between.
  • Pattern 3: print i consecutive numbers starting at row_start(i), with * between.

This abstraction is useful in placement tests. If an examiner gives a diamond variant you haven’t seen, the outer loop is already settled. The only design question is what goes inside the row.

A loop-trace table for n=4 across all three:

Row (i)Pattern 1Pattern 2Pattern 3
1111
2222*22*3
33333*3*34*5*6
444444*4*4*47*8*9*10
33333*3*34*5*6
2222*22*3
1111

Where Diamond Patterns Appear in Placement Tests

Pattern printing problems are a fixture in the programming sections of campus placement assessments at Tier-2 and Tier-3 engineering colleges across India. The diamond variant is among the harder common forms. A student who has drilled only triangle patterns will stall on the lower-half logic.

Three skills are explicitly tested by a diamond problem:

  • Outer-loop decomposition: recognising that a diamond is an upper triangle plus a lower triangle.
  • Lower-half bounds: knowing the lower half starts at n-1, not n.
  • Row-content derivation: computing what belongs on each row without memorising a formula.

Off-by-one errors are the most common failure. The lower-half loop condition i >= 1 (in C/Java) or range(n-1, 0, -1) (in Python) is the typical stall point. Tracing n=2 mentally before submitting catches this: the diamond for n=2 should print “1, 22, 1” (three rows). If the output is “1, 22, 22, 1”, the lower half starts at n instead of n-1.

For a broader set of loop-control problems that appear across IT service-tier assessments, see 20 most asked data structures interview questions. The nested-loop logic in diamond patterns connects directly to array traversal problems covered there.

Diamond patterns are also a useful warm-up before string-manipulation problems. The palindrome string check requires the same mirrored-index thinking: upper half counts up, lower half mirrors back.

GeeksforGeeks covers the full family of Python pattern programs, from inverted triangles to right-aligned variants.

The indexed loop structure in Pattern 3, a running counter that restarts at a formula-derived offset for each row, applies the same reasoning as token-sequence processing in language models. If building tools that iterate over sequences with controlled offsets is where the interest goes next, TinkerLLM connects that loop-indexed thinking to LLM application building at ₹299.

Primary sources

Frequently asked questions

How many rows does a diamond number pattern with input n have?

A diamond with input n has 2n-1 rows. The upper half runs from row 1 to row n (n rows). The lower half mirrors rows n-1 down to 1 (n-1 rows). Total: n + (n-1) = 2n-1.

What is the lower-half loop condition for a diamond pattern?

In C/C++ and Java, the lower half uses 'for (i = n - 1; i >= 1; i--)'. In Python, it is 'for i in range(n - 1, 0, -1):'. The loop starts at n-1, not n, so the widest row is printed exactly once.

How do I calculate the starting number for each row in the consecutive-number diamond?

Row k (1-indexed) starts at 1 + k*(k-1)/2. Row 1: 1+0=1. Row 2: 1+1=2. Row 3: 1+3=4. Row 4: 1+6=7. This is the triangular number formula shifted by one.

What is the difference between Pattern 1 and Pattern 2 in a diamond number program?

Both patterns print digit i on row i. Pattern 1 prints i*i digits with no separator (e.g., '333' on row 3). Pattern 2 prints them joined by '*' (e.g., '3*3*3' on row 3). The outer and inner loop logic is identical; only the print statement differs.

Do diamond pattern questions appear in campus placement tests?

Yes. Pattern programs appear in the programming section of campus assessments for Tier-2 and Tier-3 colleges, covering companies in the IT services tier. The diamond variant tests nested-loop control, off-by-one awareness, and the ability to derive loop bounds on the spot.

What is the time complexity of printing a diamond number pattern?

O(n^2). The outer loop runs 2n-1 times. The inner loop runs at most n times per row. The total number of print operations is bounded by n * (2n-1), which is O(n^2).

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF