📌 Problem Description
Input a positive integer n and output the digital pyramid in the following format (for n=5 as an example):
1 121 12321 1234321 123454321
Requirements:
-
The numbers must be arranged symmetrically.
-
Loops and conditional statements are allowed.
-
Advanced: Implement with the simplest code possible.
Difficulty: ⭐️⭐️ (suitable for learners mastering loops and formatted output).
💡 Basic Implementation (Nested Loop Method)
#include <stdio.h>
void print_pyramid(int n) { for (int i = 1; i <= n; i++) { // Print left-side spaces for (int j = 1; j <= n - i; j++) { printf(" "); } // Print left half (increasing) for (int j = 1; j <= i; j++) { printf("%d ", j); } // Print right half (decreasing) for (int j = i-1; j >= 1; j--) { printf("%d ", j); } printf("\n"); }}
int main() { int n; printf("Please enter the number of pyramid layers:"); scanf("%d", &n); print_pyramid(n); return 0;}
🔥Key Points
-
Space Control: Number of left-side spaces per line =
<span>n - i</span>
-
Number Symmetry: Handled in two parts (left half increasing, right half decreasing)
<span>⚡ Advanced Optimization (Single Loop + Mathematical Calculation)</span>
#include <stdio.h>void print_pyramid_opt(int n) { for (int i = 1; i <= n; i++) { // Calculate total characters per line (dynamically control spaces) printf("%*s", (n - i) * 2, ""); // Left-aligned spaces for (int j = 1; j < 2*i; j++) { int num = (j <= i) ? j : 2*i - j; printf("%d ", num); } printf("\n"); }}
Optimization Points:
-
Use
<span>%*s</span>
to dynamically control space width -
Single loop calculates number positions using a mathematical formula (
<span>num = j <= i ? j : 2*i - j</span>
)
⚡ Minimal Recursive Version (Challenge Code Simplification)
#include <stdio.h>
void pyramid(int i, int n) { if (i > n) return; printf("%*s", (n - i)*2, ""); for (int j = 1; j < 2*i; j++) printf("%d ", (j <= i) ? j : 2*i - j); printf("\n"); pyramid(i + 1, n);}
int main() { pyramid(1, 5); // Example: 5-layer pyramid return 0;}
Highlights:
-
Recursion replaces loops, resulting in fewer lines of code
-
Maintains clear logic while reducing variables
🤔 Common Error Analysis
Issue | Cause | Solution |
Numbers Misaligned | Multi-digit placeholders not handled | Use <span>%2d</span> or <span>%-2d</span> for fixed width |
Right Side Not Symmetrical | Incorrect starting value for decreasing loop | Check if the starting value for the right half is <span>i-1</span> |
Input 0 or Negative Number | No data validation performed | Add <span>if (n < 1) return;</span> |
🚀 Next Preview: No.7: Printing Narcissistic Numbers – Can Your Code Find All “Narcissistic Numbers”?
📢 Interaction Time
-
Can you implement the pyramid using other algorithms? Feel free to leave a comment!
-
Vote: Which implementation do you think is the most clever?
-
👍 Basic Version (Easy to Read)
-
❤️ Single Loop Version (Efficient)
-
🔥 Recursive Version (Elegant)
🚀If you find this useful, feel free to share it with friends learningC Language!
Article Author:Vv Computer Graduate World (Focusing on Computer Graduate Guidancefor 8years)
Original Statement: Please contact for authorization to reprint, infringement will be pursued.