Program to print the hollow numerical parallelogram

Program to print the hollow numerical parallelogram.
Examples :
Input : 7 Output : 7777777777777 666666 666666 55555 55555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 55555 55555 666666 666666 7777777777777
C++
// C++ program to print the given pattern #include <bits/stdc++.h> using namespace std; // Function for displaying the pattern void pattern() { // initialization int i, j, k = 0, spaces = 1, n = 7; // This will print the upper half // of the pattern for (i = n; i >= 1; i--) { for (j = 1; j <= i; j++) { cout << i; } // for printing the space characters if (i != n) { for (k = 1; k <= spaces; k++) { cout << " "; } spaces = spaces + 2; } // for displaying the corresponding // values for (j = i; j >= 1; j--) { if (j != n) cout << i; } cout << endl; } spaces = spaces - 4; // This will print the lower half // of the pattern for (i = 2; i <= n; i++) { for (j = 1; j <= i; j++) { cout << i; } // for displaying the space character // in the lower half if (i != n) { for (k = 1; k <= spaces; k++) { cout << " "; } spaces = spaces - 2; } // For displaying the corresponding // values for (j = i; j >= 1; j--) { if (j != n) cout << i; } cout << endl; } } // Driver code int main() { // Function calling pattern(); return 0; } // This code is contributed by Akanksha Rai |
C
// C program to print the given pattern #include <stdio.h> // function for displaying the pattern void pattern() { // initialization int i, j, k = 0, spaces = 1, n = 7; // This will print the upper half // of the pattern for (i = n; i >= 1; i--) { for (j = 1; j <= i; j++) { printf("%d", i); } // for printing the space characters if (i != n) { for (k = 1; k <= spaces; k++) { printf(" "); } spaces = spaces + 2; } // for displaying the corresponding // values for (j = i; j >= 1; j--) { if (j != n) printf("%d", i); } printf("\n"); } spaces = spaces - 4; // This will print the lower half // of the pattern for (i = 2; i <= n; i++) { for (j = 1; j <= i; j++) { printf("%d", i); } // for displaying the space character // in the lower half if (i != n) { for (k = 1; k <= spaces; k++) { printf(" "); } spaces = spaces - 2; } // for displaying the corresponding // values for (j = i; j >= 1; j--) { if (j != n) printf("%d", i); } printf("\n"); } } // driver code int main() { // function calling pattern(); return 0; } |
Java
// Java program to print the given pattern import java.io.*; class GFG { static void pattern() { // initialization int i, j, k = 0, spaces = 1, n = 7; // This will print the upper half // of the pattern for (i = n; i >= 1; i--) { for (j = 1; j <= i; j++) { System.out.print(i); } // for printing the space characters if (i != n) { for (k = 1; k <= spaces; k++) { System.out.print(" "); } spaces = spaces + 2; } // for displaying the corresponding // values for (j = i; j >= 1; j--) { if (j != n) System.out.print(i); } System.out.println(); } spaces = spaces - 4; // This will print the lower half // of the pattern for (i = 2; i <= n; i++) { for (j = 1; j <= i; j++) { System.out.print( i); } // for displaying the space character // in the lower half if (i != n) { for (k = 1; k <= spaces; k++) { System.out.printf(" "); } spaces = spaces - 2; } // for displaying the corresponding // values for (j = i; j >= 1; j--) { if (j != n) System.out.print(i); } System.out.println(); } } // driver code public static void main (String[] args) { // function calling pattern(); } } // This code is contributed by anuj_67. |
Python3
# Python3 program to print the given pattern # function for displaying the pattern def pattern(): # initialization k = 0 spaces = 1 n = 7 # This will print the upper half # of the pattern for i in range(n, 0, -1): for j in range(1, i + 1): print(i, end = "") # for printing the space characters if (i != n): for k in range(1, spaces + 1): print(end = " ") spaces = spaces + 2 # for displaying the corresponding # values for j in range(i, 0, -1): if (j != n): print(i, end = "") print() spaces = spaces - 4 # This will print the lower half # of the pattern for i in range(2, n + 1): for j in range(1, i + 1): print(i, end = "") # for displaying the space character # in the lower half if (i != n): for k in range(1, spaces + 1): print(end = " ") spaces = spaces - 2 # for displaying the corresponding # values for j in range(i, 0, -1): if (j != n): print(i, end = "") print() # Driver code # function calling pattern() # This code is contributed by # Mohit Kumar 29 |
C#
// C# program to print // the given pattern using System; class GFG { static void pattern() { // initialization int i, j, k = 0; int spaces = 1, n = 7; // This will print the upper // half of the pattern for (i = n; i >= 1; i--) { for (j = 1; j <= i; j++) { Console.Write(i); } // for printing the // space characters if (i != n) { for (k = 1; k <= spaces; k++) { Console.Write(" "); } spaces = spaces + 2; } // for displaying the // corresponding values for (j = i; j >= 1; j--) { if (j != n) Console.Write(i); } Console.WriteLine(); } spaces = spaces - 4; // This will print the lower // half of the pattern for (i = 2; i <= n; i++) { for (j = 1; j <= i; j++) { Console.Write(i); } // for displaying the space // character in the lower half if (i != n) { for (k = 1; k <= spaces; k++) { Console.Write(" "); } spaces = spaces - 2; } // for displaying the // corresponding values for (j = i; j >= 1; j--) { if (j != n) Console.Write(i); } Console.WriteLine(); } } // Driver Code public static void Main () { // function calling pattern(); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP program to print // the given pattern // function for displaying // the pattern function pattern() { // initialization $i; $j; $k = 0; $spaces = 1; $n = 7; // This will print the upper // half of the pattern for ($i = $n; $i >= 1; $i--) { for ($j = 1; $j <= $i; $j++) { echo $i; } // for printing the // space characters if ($i != $n) { for ($k = 1; $k <= $spaces; $k++) { echo" "; } $spaces = $spaces + 2; } // for displaying the // corresponding values for ($j = $i; $j >= 1; $j--) { if ($j != $n) echo $i; } echo"\n"; } $spaces = $spaces - 4; // This will print the lower // half of the pattern for ($i = 2; $i <= $n; $i++) { for ($j = 1; $j <= $i; $j++) { echo $i; } // for displaying the space // character in the lower half if ($i != $n) { for ($k = 1; $k <= $spaces; $k++) { printf(" "); } $spaces = $spaces - 2; } // for displaying the // corresponding values for ($j = $i; $j >= 1; $j--) { if ($j != $n) echo $i; } echo "\n"; } } // Driver Code // function calling pattern(); // This code is contributed by anuj_67. ?> |
Javascript
<script> // JavaScript program to print the given pattern // Function for displaying the pattern function pattern() { // initialization var i, j, k = 0, spaces = 1, n = 7; // This will print the upper half // of the pattern for (i = n; i >= 1; i--) { for (j = 1; j <= i; j++) { document.write(i); } // for printing the space characters if (i != n) { for (k = 1; k <= spaces; k++) { document.write(" "); } spaces = spaces + 2; } // for displaying the corresponding // values for (j = i; j >= 1; j--) { if (j != n) document.write(i); } document.write("<br>"); } spaces = spaces - 4; // This will print the lower half // of the pattern for (i = 2; i <= n; i++) { for (j = 1; j <= i; j++) { document.write(i); } // for displaying the space character // in the lower half if (i != n) { for (k = 1; k <= spaces; k++) { document.write(" "); } spaces = spaces - 2; } // For displaying the corresponding // values for (j = i; j >= 1; j--) { if (j != n) document.write(i); } document.write("<br>"); } } // Driver code // Function calling pattern(); </script> |
Output :
7777777777777 666666 666666 55555 55555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 55555 55555 666666 666666 7777777777777
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



