Applying conditional statements within nested loops opens up endless possibilities for pattern programming
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 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
Pattern Analysis & Dynamic Logic
Rows (Outer Loop): Runs from
i = 1toi = n. Columns (Inner Loop): Runs from
j = 1toj = 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 2and increment it by2for each printed value, or evaluate column indices directly.
Algorithm
Prompt the user to enter the total number of rows (
n). Execute an outer loop with control variable
irunning from1ton. Inside the outer loop, execute an inner loop with control variable
jrunning from1toi. Calculate the even number to print using the formula: val = 2 * j.
Print
valfollowed by a space. Print a newline character (
\n) after completing each row.
Code Implementation
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 rowicontains exactlyinumbers. 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
2
2 4
2 4 6
2 4 6 8
Variations & Challenges
Odd Numbers Pyramid: Modify
2 * jto(2 * j) - 1to 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 <= irather thanj < ito 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