📜  程序打印一半钻石星型图案

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

给定整数N ,任务是打印半钻石星型图案。

例子:

Input: N = 3
Output:
*
**
***
**
*

Input: N = 6
Output:
*
**
***
****
*****
******
*****
****
***
**
*

方法:想法是将模式分为上半部分和下半部分两个部分。然后在循环的帮助下分别打印。打印上半部分和下半部分的主要注意事项如下:

  • 上半部:模式的上半部按升序包含星号‘*’ ,其中i行包含以下星号:

    的“*”在第i行号= i

  • 下半部:模式的下半部按降序包含星号‘*’ ,其中i行包含以下星号:

    的“*”在第i行号= N - i

    下面是上述方法的实现:

    C++
    // C++ implementation to print the
    // half diamond star pattern
      
    #include 
      
    using namespace std;
      
    // Function to print the
    // half diamond star pattern
    void halfDiamondStar(int N)
    {
        int i, j;
      
        // Loop to print the upper half
        // diamond pattern
        for (i = 0; i < N; i++) {
            for (j = 0; j < i + 1; j++)
                cout << "*";
            cout << "\n";
        }
      
        // Loop to print the lower half
        // diamond pattern
        for (i = 1; i < N; i++) {
            for (j = i; j < N; j++)
                cout << "*";
            cout << "\n";
        }
    }
      
    // Driver Code
    int main()
    {
        int N = 5;
      
        // Function Call
        halfDiamondStar(N);
    }


    Java
    // Java implementation to print the
    // half diamond star pattern
    import java.util.*;
      
    class GFG{
      
    // Function to print the
    // half diamond star pattern
    static void halfDiamondStar(int N)
    {
        int i, j;
      
        // Loop to print the upper half
        // diamond pattern
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < i + 1; j++)
                 System.out.print("*");
            System.out.print("\n");
        }
      
        // Loop to print the lower half
        // diamond pattern
        for (i = 1; i < N; i++) 
        {
            for (j = i; j < N; j++)
                 System.out.print("*");
            System.out.print("\n");
        }
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
      
        // Function Call
        halfDiamondStar(N);
    }
    }
      
    // This code is contributed by Rohit_ranjan


    Python3
    # Python3 implementation to print the 
    # half diamond star pattern 
      
    # Function to print the 
    # half diamond star pattern 
    def halfDiamondStar(N):
          
        # Loop to print the upper half 
        # diamond pattern 
        for i in range(N):
      
            for j in range(0, i + 1):
                print("*", end = "")
            print()
      
        # Loop to print the lower half 
        # diamond pattern 
        for i in range(1, N):
      
            for j in range(i, N):
                print("*", end = "")
            print()
      
    # Driver Code 
    N = 5; 
      
    # Function Call 
    halfDiamondStar(N);
      
    # This code is contributed by skylags


    C#
    // C# implementation to print the
    // half diamond star pattern
    using System;
      
    class GFG{
      
    // Function to print the
    // half diamond star pattern
    static void halfDiamondStar(int N)
    {
        int i, j;
      
        // Loop to print the upper half
        // diamond pattern
        for(i = 0; i < N; i++)
        {
           for(j = 0; j < i + 1; j++)
               Console.Write("*");
           Console.Write("\n");
        }
      
        // Loop to print the lower half
        // diamond pattern
        for(i = 1; i < N; i++) 
        {
           for(j = i; j < N; j++)
               Console.Write("*");
           Console.Write("\n");
        }
    }
      
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 5;
      
        // Function Call
        halfDiamondStar(N);
    }
    }
      
    // This code is contributed by Rohit_ranjan


    输出:
    *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *