Monday, September 7, 2026

Printing Right-Angled Triangle of Even Numbers in C | Conditional Patterns

 Applying conditional statements within nested loops opens up endless possibilities for pattern programming. Today's article focuses on creating a Right-Angled Triangle of Even Numbers—a foundational pattern that demonstrates how simple arithmetic checks shape console output.

Pattern Title: Even Numbers Right Triangle Pattern in C

Purpose: Boost your nested loop logic and conditional reasoning by generating numerical sequences based on parity.

Prerequisites: Knowledge of basic for loops, printf, scanf, and if-else statements.

Final Output

For an input of rows = 5, the program generates:

2
2 4
2 4 6
2 4 6 8
2 4 6 8 10

Deconstructing the Pattern: The Logic

Problem Statement

Write a C program that accepts an integer n representing the total number of rows. The program must output a right-angled triangle where the ith row contains the first i even numbers.

Pattern Analysis & Dynamic Logic

  • Rows (Outer Loop): Runs from i = 1 to i = n.

  • Columns (Inner Loop): Runs from j = 1 to j = i.

  • Conditional Logic: A number is even if it is divisible by 2 (number % 2 == 0). To print even numbers sequentially, we track a counter initialized to 2 and increment it by 2 for each printed value, or evaluate column indices directly.

Algorithm

  1. Prompt the user to enter the total number of rows (n).

  2. Execute an outer loop with control variable i running from 1 to n.

  3. Inside the outer loop, execute an inner loop with control variable j running from 1 to i.

  4. Calculate the even number to print using the formula: val = 2 * j.

  5. Print val followed by a space.

  6. Print a newline character (\n) after completing each row.

Code Implementation

#include <stdio.h>

int main() {
    int n;

    // Prompt user for input
    printf("Enter the number of rows: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Please enter a valid positive integer.\n");
        return 1;
    }

    // Outer loop for rows
    for (int i = 1; i <= n; i++) {
        // Inner loop for columns in row 'i'
        for (int j = 1; j <= i; j++) {
            // Print the j-th even number
            printf("%d ", 2 * j);
        }
        // Move to the next line after finishing the row
        printf("\n");
    }

    return 0;
}

Line-by-Line Code Breakdown

  • if (scanf("%d", &n) != 1 || n <= 0): Performs input validation to catch non-integer inputs or non-positive integers.

  • for (int i = 1; i <= n; i++): Controls the row progression.

  • for (int j = 1; j <= i; j++): Ensures row i contains exactly i numbers.

  • printf("%d ", 2 * j);: Computes the even sequence directly without extra variables.

  • printf("\n");: Resets the cursor to the next line for subsequent rows.

Compiling & Execution

Sample Run

Enter the number of rows: 4
2 
2 4 
2 4 6 
2 4 6 8 

Variations & Challenges

  • Odd Numbers Pyramid: Modify 2 * j to (2 * j) - 1 to generate odd numerical triangles.

  • Alternating Parity: Print even numbers on even rows and odd numbers on odd rows by evaluating if (i % 2 == 0).

  • Inverted Form: Reverse the outer loop (for (int i = n; i >= 1; i--)) to output an inverted conditional pattern.

Common Mistakes & Troubleshooting

  • Missing Newline: Forgetting printf("\n"); causes all values to render on a single line.

  • Off-by-One Errors: Ensure your inner loop condition is j <= i rather than j < i to avoid missing the final column of each row.

  • Resetting Counters: If using a separate variable for the numbers, reset or recalculate it correctly inside the row loop.

Complexity Analysis

  • Time Complexity: {O}(n^2) due to nested outer and inner loops executing n(n+1)/2 iterations.

  • Space Complexity: {O}(1) as memory usage remains constant regardless of n.

Mastering conditional statements inside nested loops makes it much easier to handle more complex symmetric and alphabet-based patterns.

Have you tried printing an alternating even/odd triangle? Share your solution in the comments below!



For all Pattern Programs list click here

…till the next post, bye-bye & take care