Given a positive integer n. Task is to print the pyramid pattern as described in the examples below.
Examples:
Input : 2
Output :
1
22
1
Input : 3
Output :
1
22
333
22
1
Input : 5
Output :
1
22
333
4444
55555
4444
333
22
1
Algorithm:
//Print All Pattern
printPattern(int N)
// Print upper pattern
/* 1
22
333
...
N...(N)times */
for i->1 to N
for j->1 to i
print i
print next line
// Print lower triangle
/* N-1....(N-1)times
...
333
22
1 */
for i->N-1 to 0
for j->i to 0
print i
print next line
C++
// C++ program to print the pyramid pattern#include <bits/stdc++.h>using namespace std; // Print the pattern upto nvoid printPattern(int n){ // Printing upper part for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) cout << i; cout << "\n"; } // printing lower part for (int i = n - 1; i > 0; i--) { for (int j = i; j > 0; j--) cout << i; cout << "\n"; }} // Driver Codeint main(){ int n = 8; printPattern(n); return 0;} |
Java
// Java program to print the pyramid pattern class GFG { // Print the pattern upto nstatic void printPattern(int n) { // Printing upper part for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) System.out.print(i); System.out.print("\n"); } // printing lower part for (int i = n - 1; i > 0; i--) { for (int j = i; j > 0; j--) System.out.print(i); System.out.print("\n"); }} // Driver codepublic static void main(String arg[]) { int n = 8; printPattern(n);}} // This code is contributed by Anant Agarwal. |
Python3
# Python program to print# the pyramid pattern # Print the pattern upto ndef printPattern(n): # Printing upper part for i in range(n+1): for j in range(1,i+1): print(i,end="") print("") # printing lower part for i in range(n - 1,0,-1): for j in range(i,0,-1): print(i,end="") print("") # driver coden = 8printPattern(n) # This code is contributed# by Anant Agarwal. |
C#
// C# program to print the pyramid patternusing System;class GFG { // Print the pattern upto nstatic void printPattern(int n) { // Printing upper part for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) Console.Write(i); Console.WriteLine(); } // printing lower part for (int i = n - 1; i > 0; i--) { for (int j = i; j > 0; j--) Console.Write(i); Console.WriteLine(); }} // Driver codepublic static void Main() { int n = 8; printPattern(n);}} // This code is contributed by vt_m. |
PHP
<?php// php program to print the// pyramid pattern // Print the pattern upto nfunction printPattern($n){ // Printing upper part for ($i = 1; $i <= $n; $i++) { for ($j = 1; $j <= $i; $j++) echo $i; echo "\n"; } // printing lower part for ($i = $n - 1; $i > 0; $i--) { for ($j = $i; $j > 0; $j--) echo $i; echo "\n"; }} // Driver Code $n = 8; printPattern($n); // This code is contributed by mits ?> |
Output:
1 22 333 4444 55555 666666 7777777 88888888 7777777 666666 55555 4444 333 22 1
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


