Placement Prep

Pyramid Pattern Programs Using Numbers: C, Python, Java

C, C++, Java, and Python code for seven number-pyramid variants, including Floyd's triangle and Pascal's triangle, with verified output for height 5.

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

Seven distinct number-pyramid variants appear in placement coding tests, each with a different inner-loop rule: row reset, row repeat, Floyd’s triangle, inverted, centered, and Pascal’s triangle.

This article covers the number-fill variants only. The star-pattern pyramid family is a separate topic. For each number variant below, the loop structure is derived first, the output for n=5 is verified, and then complete implementations in C, C++, Python 3, and Java are provided.

Half-Pyramid Basics: Row Reset and Row Repeat

Both variants use the same outer loop running from 1 to n. The inner loop runs from 1 to i for each row i. What changes between the two is the value the inner loop prints.

Row Reset: 1, 2, 3 Per Row

The row-reset pyramid (also called the basic half-pyramid) prints 1, 2, 3 up to i for row i. The inner loop counter j serves as both the iterator and the printed value.

For n=5:

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

C:

#include <stdio.h>

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

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 << j << " ";
        cout << "\n";
    }
    return 0;
}

Python 3:

n = int(input())
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()

Java:

import java.util.Scanner;

public class HalfPyramidReset {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print(j + " ");
            System.out.println();
        }
    }
}

Row-Number Repeat: i Printed i Times

The row-repeat pyramid prints the row number i on every iteration of the inner loop. Row 1 prints one 1. Row 2 prints two 2s. Row 3 prints three 3s.

For n=5:

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

The only code change from row reset is replacing j with i in the print statement. The loop bounds j <= i remain identical.

C:

#include <stdio.h>

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

Python 3:

n = int(input())
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(i, end=' ')
    print()

The C++ and Java versions follow the same single-character substitution from the row-reset code above: replace the inner j print with i.

Floyd’s Triangle and the Sequential Count

Floyd’s triangle is a number pyramid where the counter never resets between rows. Row 1 holds 1. Row 2 continues with 2 and 3. Row 3 holds 4, 5, and 6. The k-th number in row i equals i*(i-1)/2 + k.

For n=5, verified row by row:

  • Row 1 (i=1): 1*(1-1)/2 + 1 = 1 through 1*(1-1)/2 + 1 = 1
  • Row 2 (i=2): 2*(2-1)/2 + 1 = 2 through 2*(2-1)/2 + 2 = 3
  • Row 3 (i=3): 3*(3-1)/2 + 1 = 4 through 3*(3-1)/2 + 3 = 6
  • Row 4 (i=4): 4*(4-1)/2 + 1 = 7 through 4*(4-1)/2 + 4 = 10
  • Row 5 (i=5): 5*(5-1)/2 + 1 = 11 through 5*(5-1)/2 + 5 = 15

Output for n=5:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

The formula is useful to understand, but the cleanest code uses a running counter k that increments across the entire loop without resetting. The outer loop controls row count; the inner loop fires exactly i times per row.

C:

#include <stdio.h>

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

C++:

#include <iostream>
using namespace std;

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

Python 3:

n = int(input())
k = 1
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(k, end=' ')
        k += 1
    print()

The Python range() function returns an empty sequence when the start already meets the stop condition, so the inner loop for row 1 (range(1, 2)) fires once cleanly without needing a guard.

Java:

import java.util.Scanner;

public class FloydTriangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print(k++ + " ");
            System.out.println();
        }
    }
}

Floyd’s triangle is distinct from the palindrome number pyramid: the palindrome variant resets to 1 at each row boundary and mirrors the ascending sequence back down, while Floyd’s triangle increments a single counter that runs straight through every row.

Inverted Number Pyramid

The inverted pyramid prints rows in descending order: the first output row has n numbers, each subsequent row has one fewer, and the last row has just 1. The inner loop logic is unchanged; only the outer loop direction reverses.

For n=5:

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

The outer loop runs from n down to 1. The inner loop still runs from 1 to i (the current outer-loop value), so row lengths decrease automatically.

C:

#include <stdio.h>

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

C++:

#include <iostream>
using namespace std;

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

Python 3:

n = int(input())
for i in range(n, 0, -1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()

Java:

import java.util.Scanner;

public class InvertedPyramid {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++)
                System.out.print(j + " ");
            System.out.println();
        }
    }
}

The inverted pyramid is a reliable warm-up question for checking loop-direction awareness. The most common mistake is keeping the outer loop ascending and reversing only the inner loop; that prints a standard half-pyramid, not the inverted form.

Centered Number Pyramid

The centered pyramid is the row-reset half-pyramid with leading spaces added before each row. The centering logic follows directly from the row widths.

For n=5:

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

Deriving the leading-space count

Row n (the widest, bottom row) prints n numbers each followed by a space, for a total of 2n output characters. Row i prints 2i characters. The width gap is 2*(n-i) characters. To center row i under row n, that gap is split across both sides, giving n-i spaces on the left.

This formula is derivable under exam conditions. Start from the bottom row’s width (2n), note each row above it is narrower by 2, and divide the gap by 2. The result is n-i spaces, not 2*(n-i) spaces.

Note the contrast with the palindrome number pyramid: that variant’s rows contain both an ascending and a descending sequence, so each row is much wider (width 4i-3 for row i), and the centering formula becomes 2*(n-i). The centered pyramid here contains only the ascending sequence per row, so the leading spaces are half as many.

Loop trace for n=5

Row (i)Leading spaces (n-i)Numbers printedOutput line
141 1
231 2 1 2
321 2 3 1 2 3
411 2 3 41 2 3 4
501 2 3 4 51 2 3 4 5

C:

#include <stdio.h>

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

The space loop for (j = i; j < n; j++) runs n - i times. When i = 1, it prints 4 spaces for n=5. When i = n, it runs zero times.

C++:

#include <iostream>
using namespace std;

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

Python 3:

n = int(input())
for i in range(1, n + 1):
    print(' ' * (n - i), end='')
    for j in range(1, i + 1):
        print(j, end=' ')
    print()

Java:

import java.util.Scanner;

public class CenteredPyramid {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = i; j < n; j++)
                System.out.print(" ");
            for (int j = 1; j <= i; j++)
                System.out.print(j + " ");
            System.out.println();
        }
    }
}

Pascal’s Triangle

Pascal’s triangle arranges the binomial coefficients in pyramid form. Each interior cell equals the sum of the two cells directly above it. Both boundary cells in every row are 1.

For n=5:

        1 
      1 1 
    1 2 1 
  1 3 3 1 
1 4 6 4 1 

Computing the row values

The value at row i, column j (both 0-indexed) is the combination C(i, j). Computing this via factorials at runtime leads to overflow for larger n. The standard approach builds each row from the previous: tri[i][j] = tri[i-1][j-1] + tri[i-1][j], with boundary conditions tri[i][0] = 1 and tri[i][i] = 1.

Verification for row 4 (0-indexed, which is the fifth printed row):

  • tri[4][0] = 1 (boundary)
  • tri[4][1] = tri[3][0] + tri[3][1] = 1 + 3 = 4
  • tri[4][2] = tri[3][1] + tri[3][2] = 3 + 3 = 6
  • tri[4][3] = tri[3][2] + tri[3][3] = 3 + 1 = 4
  • tri[4][4] = 1 (boundary)

Row 4 output: 1 4 6 4 1. Confirmed.

Centering Pascal’s triangle

Each number in a row is printed as a single digit followed by a space, occupying 2 characters. Row i (0-indexed) contains i+1 numbers and uses 2*(i+1) characters of width. The widest row (row n-1) uses 2n characters. Leading spaces for row i = 2*(n-1-i), printed as n-1-i pairs of two spaces.

C:

#include <stdio.h>

int main() {
    int n, i, j;
    int tri[10][10] = {0};
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        tri[i][0] = 1;
        for (j = 1; j <= i; j++)
            tri[i][j] = tri[i - 1][j - 1] + tri[i - 1][j];
        for (j = i; j < n - 1; j++)
            printf("  ");
        for (j = 0; j <= i; j++)
            printf("%d ", tri[i][j]);
        printf("\n");
    }
    return 0;
}

C++:

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int tri[10][10] = {};
    for (int i = 0; i < n; i++) {
        tri[i][0] = 1;
        for (int j = 1; j <= i; j++)
            tri[i][j] = tri[i - 1][j - 1] + tri[i - 1][j];
        for (int j = i; j < n - 1; j++)
            cout << "  ";
        for (int j = 0; j <= i; j++)
            cout << tri[i][j] << " ";
        cout << "\n";
    }
    return 0;
}

Python 3:

n = int(input())
tri = [[0] * n for _ in range(n)]
for i in range(n):
    tri[i][0] = 1
    for j in range(1, i + 1):
        tri[i][j] = tri[i - 1][j - 1] + tri[i - 1][j]
    print("  " * (n - 1 - i), end="")
    for j in range(i + 1):
        print(tri[i][j], end=" ")
    print()

Java:

import java.util.Scanner;

public class PascalTriangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] tri = new int[n][n];
        for (int i = 0; i < n; i++) {
            tri[i][0] = 1;
            for (int j = 1; j <= i; j++)
                tri[i][j] = tri[i - 1][j - 1] + tri[i - 1][j];
            for (int j = i; j < n - 1; j++)
                System.out.print("  ");
            for (int j = 0; j <= i; j++)
                System.out.print(tri[i][j] + " ");
            System.out.println();
        }
    }
}

Pattern Programs in Campus Placement Tests

Number-pyramid programs appear in the first coding screen of campus placement assessments, particularly at mass-recruiting IT services firms and at Tier-2 and Tier-3 colleges where the test batteries typically include one or two pattern questions alongside standard loops-and-arrays problems.

Floyd’s triangle is the variant most likely to trip candidates. The error is almost always the same: treating each row as independent and resetting the counter instead of carrying it forward. A quick self-check before submitting is to verify that the first number of row i equals i*(i-1)/2 + 1 (the formula derived above). For row 5, that check gives 11, not 1.

Pascal’s triangle appears less often in mass-screening rounds and more frequently in the problem-solving sections of product-company assessments, where the DP row-build approach is the expected solution rather than computing raw factorials.

For students working through IT fresher coding assessments, pattern programs fall into the programming-fundamentals bucket. They are solvable quickly once the loop structure is clear, but that clarity requires tracing at least one small case (n=3 or n=4) by hand before writing code.

GeeksforGeeks covers the extended family of pyramid programs across multiple languages if you want additional pattern variants beyond those here.

The running counter in Floyd’s triangle, maintained across nested loops and never reset to 1, is the same structural pattern that drives token ID sequences and batch indexing in LLM tools. TinkerLLM is where that loop logic gets applied to building real LLM-powered applications; the entry point is ₹299.

Primary sources

Frequently asked questions

How does Floyd's triangle differ from a basic number pyramid?

Floyd's triangle uses a running counter that never resets between rows. Each new row continues from where the previous row left off. A basic pyramid resets the counter to 1 at the start of every row, so each row prints 1, 2, 3, up to the row number.

What is the leading-space formula for the centered number pyramid?

For row i in an n-row centered pyramid, the number of leading spaces equals n minus i. The bottom row has width 2n characters (n numbers each followed by a space); row i has width 2i characters. The difference is 2*(n-i), which splits to n-i spaces on the left side.

How do you compute Pascal's triangle without using factorials?

Build each row from the previous using the recurrence cell[i][j] = cell[i-1][j-1] + cell[i-1][j], with boundary values cell[i][0] = cell[i][i] = 1. This runs in O(n squared) time and avoids factorial overflow for moderate n.

Where does the row-repeat pattern differ from the row-reset pattern in code?

In row reset, the inner loop prints its own counter j, giving 1, 2, 3 up to i for row i. In row repeat, the inner loop prints the outer-loop counter i on every iteration, so row i prints the number i exactly i times. The loop bounds are identical; only the printed value differs.

Which number-pyramid patterns appear most often in campus coding tests?

Floyd's triangle and the basic row-reset half-pyramid appear most frequently in campus coding rounds. Pascal's triangle appears in problem-solving rounds at product-company assessments. The centered variant tests whether students can derive a centering formula under time pressure.

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