📜  程序打印空心金字塔和菱形图案

📅  最后修改于: 2021-05-30 03:29:56             🧑  作者: Mango

先决条件:循环,如果其他语句
1.空心金字塔/三角形图案
该图案类似于金字塔图案。唯一的区别是,我们将通过空格字符和替换所有内部“#”字符我们将2 * N-1(N =在图案行数)“#”最后一行中的字符。
例子:

Input rows: 6
Output:
     #
    # #
   #   #
  #     #
 #       #
#         #
###########    
C++
// CPP program to print a hollow pyramid pattern
#include 
using namespace std;
void printPattern(int);
int main()
{
    int n = 6;
 
    printPattern(n);
}
void printPattern(int n)
{
    int i, j, k = 0;
    for (i = 1; i <= n; i++) // row=6
    {
        // Print spaces
        for (j = i; j < n; j++) {
            cout << " ";
        }
        // Print #
        while (k != (2 * i - 1)) {
            if (k == 0 || k == 2 * i - 2)
                cout << "#";
            else
                cout << " ";
            k++;
            ;
        }
        k = 0;
        cout << endl; // print next row
    }
    // print last row
    for (i = 0; i < 2 * n - 1; i++) {
        cout << "#";
    }
}


Java
// JAVA program to print a hollow
// pyramid pattern
class GFG{
     
    public static void main(String args[])
    {
        int n = 6;
     
        printPattern(n);
    }
     
    static void printPattern(int n)
    {
        int i, j, k = 0;
        for (i = 1; i <= n; i++) // row=6
        {
            // Print spaces
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
            // Print #
            while (k != (2 * i - 1)) {
                if (k == 0 || k == 2 * i - 2)
                    System.out.print("#");
                else
                    System.out.print(" ");
                k++;
                ;
            }
            k = 0;
             
            // print next row
            System.out.println();
        }
        // print last row
        for (i = 0; i < 2 * n - 1; i++) {
            System.out.print("#");
        }
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python
# Python program to print a hollow
# pyramid pattern
 
def printPattern( n) :
    k = 0
    for i in range(1,n+1) : #row 6
     
        # Print spaces
        for j in range(i,n) :
            print(' ', end='')
         
        # Print #
        while (k != (2 * i - 1)) :
            if (k == 0 or k == 2 * i - 2) :
                print('#', end='')
            else :
                print(' ', end ='')
            k = k + 1
        k = 0;
        print ("") # print next row
         
    # print last row
    for i in range(0, 2 * n -1) :
        print ('#', end = '')
 
# Driver code
n = 6
printPattern(n)
 
# This code is contributed by Nikita Tiwari.
c#] 


Javascript


输出:

#
    # #
   #   #
  #     #
 #       #
#         #
 #         #
  #       #
   #     #
    #   #
     # #
      #
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。