This tutorial will guide you through creating a full pyramid pattern using numbers in C. This is a more advanced pattern programming exercise that builds on your understanding of nested loops and requires careful control of spaces and numbers to achieve the desired symmetrical shape.
Final Output
The final output is a number pyramid where each row is centered and contains a sequence of numbers that first increase and then decrease. For a user input of 5
, the output will look like this:
Enter the number of rows: 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Deconstructing the Pattern: The Logic
The full pyramid pattern is created by combining the logic of a left-aligned triangle (for the increasing numbers) and a right-aligned triangle (for the decreasing numbers), all while being centered by printing leading spaces.
Problem Statement: The program should take an integer input n
from the user and print a symmetrical number pyramid pattern.
Pattern Analysis & Logic:
Outer Loop (Rows): The pattern has
n
rows, so our outer loop will run fromi = 1
ton
. This loop controls the row number.First Inner Loop (Spaces): To center the pyramid, we need to print leading spaces. The number of spaces decreases as the row number increases. The formula is
(n - i)
. So, this loop will run fromj = 1
to(n - i)
.Second Inner Loop (Increasing Numbers): The numbers on the left side of the pyramid increase from
1
up to the current row numberi
. This loop will run fromj = 1
toi
, printing the value ofj
.Third Inner Loop (Decreasing Numbers): The numbers on the right side of the pyramid decrease from
(i - 1)
down to1
. This loop will run fromj = (i - 1)
down to1
, printing the value ofj
.
Here is the step-by-step breakdown of the algorithm:
Start the program.
Declare integer variables
n
,i
, andj
.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 the first inner
for
loop that runs fromj = 1
to(n - i)
to print the leading spaces.Create a second inner
for
loop that runs fromj = 1
toi
to print the increasing numbers on the left side of the pyramid.Create a third inner
for
loop that runs fromj = (i - 1)
down to1
to print the decreasing numbers on the right side of the pyramid.After the inner loops finish, use
printf("\n");
to move to the next line.Repeat the process until the outer loop completes.
The Code Implementation
Here is the complete C program with inline comments to explain the purpose of each loop.
#include <stdio.h>
int main() {
// Declare variables
int i, j, n;
// Prompt user for input
printf("Enter the number of rows: ");
scanf("%d", &n);
// Outer loop for rows
for (i = 1; i <= n; i++) {
// Inner loop for leading spaces
for (j = 1; j <= n - i; j++) {
printf(" ");
}
// Inner loop for increasing numbers on the left side
for (j = 1; j <= i; j++) {
printf("%d", j);
}
// Inner loop for decreasing numbers on the right side
for (j = i - 1; j >= 1; j--) {
printf("%d", j);
}
// Move to the next line
printf("\n");
}
return 0;
}
Explanation:
for (i = 1; i <= n; i++)
: This is the outer loop that controls the total number of rows.for (j = 1; j <= n - i; j++)
: This first inner loop is responsible for printing the leading spaces. The number of spaces decreases asi
(the row number) increases, creating the pyramid shape.for (j = 1; j <= i; j++)
: The second inner loop prints the increasing numbers from 1 up to the current row numberi
.for (j = i - 1; j >= 1; j--)
: The third inner loop prints the decreasing numbers. It starts from(i - 1)
to avoid repeating the peak number of the row, and counts down to 1.printf("\n");
: This statement moves the cursor to the next line after each row is completely printed.
Sample Output
Here is a dry-run table for a user input of n=3
.
i (Rows) | Spaces (n-i ) | j (increasing) | Output | j (decreasing) | Output | Total Output |
1 | 2 | j=1 | 1 | - | - | 1 |
2 | 1 | j=1,2 | 12 | j=1 | 1 | 121 |
3 | 0 | j=1,2,3 | 123 | j=2,1 | 21 | 12321 |
Variations and Enhancements
Add Spaces between Numbers: You can add spaces between the numbers (e.g.,
printf("%d ", j);
) to make the pattern more spread out and readable.Change the Character: You can easily change the
printf
statement to print stars (*
), letters, or other symbols instead of numbers.Challenge: Try to modify the code to create a full diamond pattern by combining a regular pyramid and an inverted pyramid.
Common Mistakes and Troubleshooting
Incorrect Loop Conditions: Ensuring your loop conditions are correct is crucial. A small typo can completely change the pattern. Double-check the conditions for
n-i
andi-1
.Forgetting
\n
: A common mistake is forgetting theprintf("\n");
statement. Without it, the entire pattern will be printed on a single line.
Complexity Analysis
Time Complexity: The time complexity is O(n^2) because of the nested loops. The total number of operations is roughly proportional to the square of
n
.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.
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 Numbers in a Row | Number Patterns in C
Conclusion
You have successfully learned how to build a full number pyramid pattern. This exercise demonstrates the power of nested loops and the importance of carefully managing each part of your program to create a complex, symmetrical shape.
....till next post, bye-bye & take care!
No comments:
Post a Comment