Программа для печати полого числового параллелограмма

Опубликовано: 5 Марта, 2022

Программа для печати полого числового параллелограмма.
Примеры :

 Ввод: 7
Выход :
7777777777777
666666 666666
55555 55555
4444 4444
333 333
22 22
1 1
22 22
333 333
4444 4444
55555 55555
666666 666666
7777777777777

Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.

 

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(" ");
    }
    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(" ");
    }
}
 
// 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);
        &nbs