Integrating conditional statements into dynamic loop structures allows you to manipulate matrix-like numerical outputs with precision
Pattern Title: Alternating Even/Odd Number Triangle Pattern in C
Purpose: Deepen your understanding of nested loops, modulus operations, and row-level parity switching
Prerequisites: Familiarity with nested for loops, printf, scanf, and basic if-else control flow
Final Output
For an input of n = 5 rows, the program generates
1 3
2 4 6
1 3 5 7
2 4 6 8 10
Deconstructing the Pattern: The Logic
Problem Statement
Write a C program that accepts an integer n for the total number of rows1
Pattern Analysis & Dynamic Logic
Row Loop (Outer Loop):
iruns from1ton. Column Loop (Inner Loop):
jruns from1toi, dictating the total numbers printed per row. Conditional Parity Check:
If the row index
iis odd, print the jth even number: 2 \times j.If the row index
iis even, print the jth odd number: (2 \times j) - 1.
Step-by-Step Algorithm
Read the positive integer
nfrom the user. Iterate
ifrom1up tonfor row placement. For each row
i, iteratejfrom1toi. Evaluate
i % 2:If non-zero (odd row), evaluate value as
2 * j. If zero (even row), evaluate value as
(2 * j) - 1.
Print the calculated number followed by a space
. Print a newline (
\n) at the end of every row iteration.
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++) {
// Check row index parity
if (i % 2 != 0) {
// Odd rows print even numbers
printf("%d ", 2 * j);
} else {
// Even rows print odd numbers
printf("%d ", (2 * j) - 1);
}
}
// Move to the next line after completing the row
printf("\n");
}
return 0;
}
Line-by-Line Code Breakdown
if (scanf("%d", &n) != 1 || n <= 0): Performs strict input validation to guard against non-numeric or non-positive input values. for (int i = 1; i <= n; i++): Dictates row progression from top to bottom. if (i % 2 != 0): Determines whether the current row index is odd or even. printf("%d ", 2 * j);: Computes standard even numbers sequentially per column. printf("%d ", (2 * j) - 1);: Computes standard odd numbers sequentially per column. printf("\n");: Terminates the current printed line to build the triangular shape.
Compiling & Execution
Sample Output
2
1 3
2 4 6
1 3 5 7
Variations & Enhancements
Column-Based Alternating: Replace row checks (
i % 2) with column checks (j % 2) to alternate numbers across columns within the exact same row. Inverted Alternating Triangle: Reverse the outer loop (
for (int i = n; i >= 1; i--)) to print the largest row at the top. Continuous Global Counter: Instead of resetting calculations per row using
j, maintain global even and odd counters across the entire execution.
Common Mistakes & Troubleshooting
Confusing Row (
i) vs. Column (j) Parity: Checkingj % 2instead ofi % 2changes the output from alternating rows to alternating columns. Incorrect Odd Formula: Using
2 * j + 1skips1and starts odd rows at3. Ensure you use(2 * j) - 1whenjstarts at1. Missing Line Break: Forgetting
printf("\n");renders all numbers into a single continuous stream.
Complexity Analysis
Time Complexity: {O}(n^2) — requires n(n+1)/2 total operations across outer and inner loops
. Space Complexity: {O}(1) — executes in constant space using basic scalar variables
.
Mastering parity-based conditional checks inside nested structures gives you complete control over complex matrix layouts
Try rewriting this program so odd rows print odd numbers and even rows print even numbers! Share your code modifications in the comments below
For all Pattern Programs list click here:
…till the next post, bye-bye & take care
No comments:
Post a Comment