📜  打印空心数值平行四边形的程序

📅  最后修改于: 2022-05-13 01:57:58.581000             🧑  作者: Mango

打印空心数值平行四边形的程序

打印空心数字平行四边形的程序。
例子 :

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


输出 :
7777777777777
666666 666666
55555   55555
4444     4444
333       333
22         22
1           1
22         22
333       333
4444     4444
55555   55555
666666 666666
7777777777777