Placement Prep

Pattern Printing in C: 9 Interview Programs Explained

Nine pattern printing programs for C technical interviews: star, number, and letter types with verified code, output traces, and complexity notes.

By FACE Prep Team 7 min read
c-programming technical-interview pattern-printing placement-prep loops coding-interview

Pattern printing programs test three interview fundamentals in one question: nested loop control, index arithmetic, and output formatting.

Service-company placement tests at TCS, Infosys, and Wipro include at least one pattern program in the coding section. Product-company rounds ask for the same code and then add a follow-up: state the time complexity and name one edge case. The nine programs below cover every variant that appears in placement rounds, with code that has been traced to verified output before appearing here.

If you want a broader set of logic programs beyond patterns, the 31 most-asked C programming interview questions and programs guide covers swap, palindrome, linked-list reversal, and pointer-arithmetic problems in the same depth.


Why Pattern Printing Programs Appear in C Interviews

Three concrete reasons pattern programs appear consistently in technical rounds:

  • Nested loop reasoning. A pattern program requires two loops where the inner loop’s bound depends on the outer loop’s counter. Interviewers use this to check whether a candidate understands loop-variable relationships before touching pointers or recursion.
  • Output formatting. Getting printf to place a space between stars (but not after the last one, or consistently after every star) is a small but concrete test of attention to output format. The C for statement specification on cppreference documents exactly how the init, condition, and increment expressions execute — worth reading once before any coding round.
  • Index arithmetic under pressure. The full pyramid requires computing both the number of leading spaces and the number of stars per row as functions of the row number. That arithmetic, done correctly in under 10 minutes, is what separates prepared candidates from unprepared ones.

All nine patterns below run in O(n²) time with O(1) auxiliary space. When an interviewer says “now analyse the complexity”, that is the correct answer for every single one of them.


Star Pattern Programs

Half Pyramid

The simplest pattern and the starting point for every nested-loop interview question. Row i prints i stars.

#include <stdio.h>

int main() {
    int n = 4, i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Output (n = 4):

* 
* * 
* * * 
* * * * 

Key loop bound: inner condition is j <= i. The most common mistake is writing j < i (which prints one fewer star per row) or j <= n (which prints a full rectangle instead of a triangle).

Inverted Half Pyramid

Same structure, outer loop counts down. Row i (starting from n) prints i stars.

#include <stdio.h>

int main() {
    int n = 4, i, j;
    for (i = n; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Output (n = 4):

* * * * 
* * * 
* * 
* 

The outer loop’s direction is the only change from the half pyramid. Interviewers sometimes ask for both back-to-back to check whether a candidate can invert a loop without restructuring the entire function.

Full (Centred) Pyramid

This is the variant that trips up most candidates at service-company coding tests. Each row needs (n - i) leading spaces and (2 * i - 1) stars.

#include <stdio.h>

int main() {
    int n = 4, i, j;
    for (i = 1; i <= n; i++) {
        for (j = i; j < n; j++) {
            printf(" ");
        }
        for (j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output (n = 4):

   *
  ***
 *****
*******

Trace the arithmetic for row 1: (n - 1) = 3 spaces, (2 * 1 - 1) = 1 star. For row 4: 0 spaces, (2 * 4 - 1) = 7 stars. Writing this derivation on the whiteboard before coding is what interviewers mean by “show your reasoning”.


Number Pattern Programs

Floyd’s Triangle

Floyd’s triangle fills rows with consecutive integers. A single counter variable k increments across all rows and inner iterations.

#include <stdio.h>

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

Output (n = 4):

1 
2 3 
4 5 6 
7 8 9 10 

The counter k is declared outside both loops and incremented in the printf call using the post-increment operator. This is the idiomatic form; initialising k inside the outer loop is a common mistake that resets the counter per row.

Pascal’s Triangle

Each cell contains the sum of the two cells directly above it. The code below builds the triangle row by row using a 2D array.

#include <stdio.h>

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

Output (n = 5):

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

The two boundary assignments C[i][0] = 1 and C[i][i] = 1 must happen before the inner fill loop. A candidate who misses those two lines produces incorrect output for every row after the first.

Number Pyramid

Row i prints integers from 1 to i. This is the number analogue of the half pyramid.

#include <stdio.h>

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

Output (n = 4):

1 
1 2 
1 2 3 
1 2 3 4 

The only difference from Floyd’s triangle is that the inner loop variable j is printed rather than an external counter k. Interviewers sometimes ask both back-to-back to check whether the candidate understands why the two outputs differ.

Reverse Number Triangle

The outer loop starts at n and counts down. Each row still prints 1 through the current row value.

#include <stdio.h>

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

Output (n = 4):

1 2 3 4 
1 2 3 
1 2 
1 

Letter Patterns and the Hollow Square

Letter Pyramid

Characters are printed using ASCII arithmetic: 'A' + j gives the character at offset j from ‘A’. Row i (zero-indexed) prints ‘A’ through the character at offset i.

#include <stdio.h>

int main() {
    int n = 4, i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j <= i; j++) {
            printf("%c ", 'A' + j);
        }
        printf("\n");
    }
    return 0;
}

Output (n = 4):

A 
A B 
A B C 
A B C D 

The zero-based indexing (i from 0 to n-1, j from 0 to i) is the natural form for character offset arithmetic. Candidates who use 1-based indexing here often produce off-by-one errors on the character offset.

Hollow Square

The hollow square is the entry point for conditional logic inside nested loops. A star is printed only when the cell is on the border: first row, last row, first column, or last column. Interior cells print two spaces to maintain alignment.

#include <stdio.h>

int main() {
    int n = 5, i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n) {
                printf("* ");
            } else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}

Output (n = 5):

* * * * * 
*       * 
*       * 
*       * 
* * * * * 

The compound condition i == 1 || i == n || j == 1 || j == n is the border-check. This structure directly maps to any “print only the boundary” problem: hollow rectangle, hollow triangle, or border-only matrix traversal. Internalise this condition once; it transfers to every variant.


What Interviewers Look for Beyond the Output

Getting the code to compile and produce correct output is necessary. At service-company level (TCS, Capgemini, Wipro), that is sufficient to pass the coding round. At product-company level, three more things matter.

Complexity statement. Every pattern above is O(n²) time, O(1) space. State that proactively. Interviewers at product companies explicitly ask “what is the time complexity”, not as a trick but to gauge whether you separate problem-solving from analysis.

Edge cases. What happens when n = 1? For the half pyramid, the output is a single star. For the hollow square, all four border conditions are true simultaneously: i == 1 and i == n and j == 1 and j == n for the only cell. Mentioning this shows the interviewer you’ve thought beyond the happy path.

printf format specifiers. The cppreference printf reference documents every format specifier. In an interview, the difference between %d and %c, or between printing "* " and "*\n", is what separates candidates who understand output formatting from those who guess. For conceptual questions about pointer semantics and memory layout that appear at the same interview stage, the must-solve conceptual C programming questions guide covers null pointers, dangling pointers, and sizeof output prediction.

The nested loops in every pattern above share a structural property. At each grid position (i, j), the program checks a condition and emits a character, then advances. That same iterate-and-decide structure is how large language models generate output token by token: outer position loop, inner logit calculation, one token emitted per step. TinkerLLM (₹299) puts that loop in your hands via API, callable from C or Python. If the nested-loop structure is already familiar, seeing it at the LLM level is a natural next step.

Primary sources

Frequently asked questions

How many rows should I use when tracing a pattern in an interview?

Use 3 or 4 rows when tracing on paper or whiteboard. Three rows are enough to reveal the loop-bound relationship without running out of space, and a 4-row trace confirms the off-by-one edge case.

Can I use a while loop instead of a for loop for pattern programs?

Yes. A while loop is logically equivalent to a for loop for any pattern program. The for loop is conventional for patterns because the init, condition, and increment are all visible on one line, which makes the loop bound easier to audit quickly. Either form is accepted in interviews.

What is the time complexity of pattern printing programs in C?

All nine patterns in this guide run in O(n²) time, where n is the number of rows. The outer loop runs n times and the inner loop runs at most n times per outer iteration. Auxiliary space is O(1) — only the loop counters and a running counter for Floyd's triangle are needed.

Why do interviewers ask pattern printing in technical rounds?

Pattern programs test whether a candidate can reason about two nested loop variables simultaneously and translate a visual grid into index arithmetic. That skill transfers directly to matrix traversal, 2D array manipulation, and grid-based dynamic programming — problems that appear in product-company interviews.

What is the difference between Floyd's triangle and Pascal's triangle?

Floyd's triangle fills rows with consecutive integers starting from 1 — the first row has 1, the second has 2 and 3, the third has 4, 5, and 6, and so on. Pascal's triangle fills each cell with the sum of the two cells directly above it, producing binomial coefficients. Floyd's triangle is simpler to code; Pascal's triangle requires either a 2D array or the previous row.

Is the diamond pattern asked separately from pyramid patterns?

Yes. The diamond pattern is treated as its own program in most placement rounds. It combines an upper pyramid and an inverted pyramid, so it is more involved than either alone. Once you are comfortable with the full pyramid here, the diamond variant is a natural next step.

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