This tutorial will teach you how to create a half pyramid number pattern in C, a fundamental exercise for beginners to master nested loops and pattern-building logic. By the end, you'll be able to create this classic pattern and understand the core concepts behind it.
Final Output
The final output is a number triangle where each row prints a sequence of numbers up to the row number itself. For a user input of 5
, the output will look like this:
Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Deconstructing the Pattern: The Logic
Creating a pattern requires breaking it down into rows and columns
Problem Statement: The program should take an integer input n
from the number of rows from the user and print a number triangle pattern where each row contains numbers from 1 up to the current row number.
Pattern Analysis & Logic:
Identifying the Rows: The pattern has
n
rows, so our outer loop will run fromi = 1
ton
2 . This loop controls the row number.Identifying the Columns: The number of characters in each row is equal to the row number
3 . This means our inner loop will run fromj = 1
toi
4 . This loop controls the numbers printed on each row.Printing the Numbers: Inside the inner loop, we will print the value of
j
. Since the inner loop's counterj
starts at 1 and goes up toi
, this naturally prints the sequence 1, 1 2, 1 2 3, and so on.
Here is the step-by-step breakdown of the algorithm:
Start the program.
Declare an integer variable
n
for the number of rows.Prompt the user to enter the number of rows.
Read the user's input and store it in
n
.Create an outer
for
loop that iterates fromi = 1
ton
(for the rows).Inside the outer loop, create an inner
for
loop that iterates fromj = 1
toi
(for the columns).Inside the inner loop, print the value of
j
followed by a space.After the inner loop finishes (meaning a full row has been printed), use
printf("\n");
to move to the next line5 .Repeat the process until the outer loop completes.
The Code Implementation
Here is the complete C program, with inline comments to explain each key line of code
#include <stdio.h>
int main() {
// Declare variables for rows and user input
int i, j, n;
// Prompt the user for the number of rows
printf("Enter the number of rows: ");
scanf("%d", &n);
// Outer loop for the rows
for (i = 1; i <= n; i++) {
// Inner loop for the columns
for (j = 1; j <= i; j++) {
// Print the number and a space
printf("%d ", j);
}
// Move to the next line after each row is complete
printf("\n");
}
return 0;
}
Explanation:
int i, j, n;
: We declare three integer variables:n
to store the user input,i
for the outer loop (rows), andj
for the inner loop (columns)7 .for (i = 1; i <= n; i++)
: This outer loop iterates from 1 up to the number of rows entered by the user. In each iteration, it represents a new row8 .for (j = 1; j <= i; j++)
: This inner loop runs for each row. The conditionj <= i
ensures that the number of elements printed in each row is equal to the current row numberi
9 .printf("%d ", j);
: This statement prints the value ofj
followed by a space. Sincej
increments in the inner loop, it prints1
, then1 2
, then1 2 3
, and so on.printf("\n");
: After the inner loop completes its execution for a given row, this statement moves the cursor to the next line, preparing for the next row of the pattern10 .
Sample Output
Here is an example of what you'll see when you compile and run the program with a sample input.
Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Output Analysis:
Row 1 (i=1): The inner loop runs from
j=1
to1
, printing1
. A newline is then printed.Row 2 (i=2): The inner loop runs from
j=1
to2
, printing1 2
. A newline is then printed.Row 3 (i=3): The inner loop runs from
j=1
to3
, printing1 2 3
. A newline is then printed.The pattern continues this way until
i
reaches5
.Dry-Run Table
A dry-run table is an excellent way to show how the variables change during each iteration of the loop.
Dry-Run Table of Printing Number Triangle (Half Pyramid) | Number Patterns in C
Variations and Enhancements
Change the Logic: Modify the
printf
statement to printi
instead ofj
11 . This will produce a pattern where each row prints the row number repeatedly (e.g.,1
,2 2
,3 3 3
).Modify the Character: Suggest how to convert the pattern to a star, hollow, mirrored, or alphabet version
12 .Challenge: Try to modify the code to create an inverted version of this pattern
13 .
Common Mistakes and Troubleshooting
Incorrect Loop Conditions: A small typo in the loop conditions, such as
i < n
, can completely change the pattern14 . Ensure your loops run for the correct range.Missing Newline: Forgetting the
printf("\n");
statement will cause all the numbers to print on a single line, making a horizontal pattern instead of a triangle15 .
Complexity Analysis
Time Complexity: The time complexity is O(n^2) because of the nested loops. The inner loop's execution depends on the outer loop, so the total number of operations is roughly proportional to the square of
n
16 .Space Complexity: The space complexity is O(1) as the program uses a constant amount of memory for variables, regardless of the number of rows entered by the user
17 .
Conclusion
You have successfully learned how to build a number triangle pattern using nested loops. This exercise reinforces the critical concepts of loop control and pattern analysis, which are the building blocks for more complex programming challenges.
....till next post, bye-bye & take care!
No comments:
Post a Comment