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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.