📜  打印金字塔图案的程序

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

打印金字塔图案的程序

编写程序打印由星星组成的金字塔图案

例子 :

Input:  n = 6
Output:
       *
       * *
       * * *
       * * * *
       * * * * *
       * * * * * * 
       * * * * *
       * * * *
       * * *
       * * 
       *

我们强烈建议您最小化您的浏览器并首先自己尝试。
这个想法是为金字塔的每个部分使用两个 for 循环。这两部分可分为上半部分和下半部分

C++
// C++ program to print Pyramid pattern
#include
using namespace std;
 
void pattern(int n)
{   
    // For printing the upper part of the pyramid
    for (int i = 1; i < n; i++){
        for (int j = 1; j < i+1; j++){
            cout <<" * ";
        }
        cout << endl ;
    }
 
    // For printing the lower part of pyramid
    for (int i = n; i > 0; i--){
        for (int j = i; j > 0; j--){
            cout <<  " * ";
        }
        cout << endl ;
    }
}
 
// Driver program
int main()
{
    pattern(6);
    return 0;
}


Java
// Java program to print Pyramid pattern
import java.io.*;
 
class GFG
{
    public static void pattern(int n)
    {
        // For printing the upper
        // part of the pyramid
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < i+1; j++)
            {
                System.out.print(" * ");
            }
            System.out.println();
        }
     
        // For printing the lower
        // part of pyramid
        for (int i = n; i > 0; i--)
        {
            for (int j = i; j > 0; j--)
            {
                System.out.print(" * ");
            }
            System.out.println();
        }
    }
     
    // Driver program
    public static void main(String args[])
    {
        pattern(6);
    }
}
     
// This code is contributed by NIkita Tiwari.


Python3
# Python program to print Pyramid pattern
 
def pattern(n):
    
    # For printing the upper part of pyramid
    for i in range (1, n+1):
        for j in range (1, i+1):
            print (" *",end=" ")
        print()
     
    # for printing the middle and lower part of pyramid
    for i in range (n, 1, -1):
        for j in range (i, 1, -1):
            print (" *",end=" ")
        print()
 
# Driver program
pattern(6)


C#
// C# program to print Pyramid pattern
using System;
 
class GFG {
    public static void pattern(int n)
    {
        // For printing the upper
        // part of the pyramid
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < i + 1; j++) {
                Console.Write(" * ");
            }
            Console.WriteLine();
        }
 
        // For printing the lower
        // part of pyramid
        for (int i = n; i > 0; i--) {
            for (int j = i; j > 0; j--) {
                Console.Write(" * ");
            }
            Console.WriteLine();
        }
    }
 
    // Driver program
    public static void Main()
    {
        pattern(6);
    }
}
 
// This code is contributed by vt_m.


PHP
 0; $i--)
    {
        for ($j = $i; $j > 0; $j--)
        {
            echo " * ";
        }
        echo "\n" ;
    }
}
 
// Driver code
$n=6;
pattern($n);
 
// This code is contributed by mits
?>


Javascript


输出 :

*
 *  *
 *  *  *
 *  *  *  *
 *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *
 *  *  *  *
 *  *  *
 *  *
 *