C Program
#include <stdio.h> int main(){ int n=5; for(int i=1;i<=n;i++){ for(int s=1;s<=n-i;s++) printf(" "); for(int j=i;j>=1;j--) printf("%d ",j); for(int j=2;j<=i;j++) printf("%d ",j); printf("\n"); } }
C Output
Input: n = 5 Output: 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5
C++ Program
#include <iostream> using namespace std; int main(){ int n=4; for(int i=1;i<=n;i++){ for(int s=0;s<n-i;s++) cout<<" "; for(int j=i;j>=1;j--) cout<<j<<" "; for(int j=2;j<=i;j++) cout<<j<<" "; cout<<"\n"; } }
C++ Output
Input: n = 4 Output: 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4
JAVA Program
public class Main{ public static void main(String[] args){ int n=6; for(int i=1;i<=n;i++){ for(int s=0;s<n-i;s++) System.out.print(" "); for(int j=i;j>=1;j--) System.out.print(j+" "); for(int j=2;j<=i;j++) System.out.print(j+" "); System.out.println(); } } }
JAVA Output
Input: n = 6 Output: 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6
Python Program
n=3 for i in range(1,n+1): print(" "*(n-i) + " ".join(str(j) for j in range(i,0,-1)) + " " + " ".join(str(j) for j in range(2,i+1)) if i>1 else " "*(n-i)+"1 ")
Python Output
Input: n = 3 Output: 1 2 1 2 3 2 1 2 3
Deep-Dive Explanation
Explanation of the Palindromic Pyramid
The palindromic pyramid is a symmetric pattern in which every row is a list of numbers that initially degrades to 1 and subsequently rises again up to the row number. The preceding spaces form the centered pyramid.
Example
For n = 4:
Row 1 → " 1" (centered)
Row 2 → " 2 1 2"
Row 3 → " 3 2 1 2 3"
Row 4 → "4 3 2 1 2 3 4"
The pattern in each row is a palindrome — it reads the same backward as forward.
Real-Life Analogy
Imagine a pendulum swinging. It begins at a high number, swings down to the lowest point (1), and then swings back up symmetrically. This movement is precisely what's occurring in the number pattern.
Why It Matters
This exercise educates you on loop nesting, symmetry logic, and handling number sequences. You also understand string alignment methods, critical in developing neatly formatted console output and user interfaces.
Learning Insights
The exercise supports:
The mirroring of sequences through loops.
Two-part printing logic (decreasing left, increasing right).
Handling spaces for alignment.
Understand palindromic attributes in patterns.
Interview & Real-World Use
In interviews, such a problem ensures that you are able to divide a visual pattern into two rational halves and make effective use of loops. In actual scenarios, the same principle also holds true while designing symmetrical data visualizations, dashboards, or generating text art.
The palindromic pyramid code in C, C++, Java, and Python illustrates the application of symmetry and mathematics to nested loops in order to produce beautiful number patterns. Learning it lays the ground for solving intricate pattern problems during coding interviews and aiding in designing aesthetically pleasing console-based programs. It's an ideal step toward achieving mastery in control flow in programming as well as improving logical thinking skills for developers.
Social Plugin