📜  六边形图案程序

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

六边形图案程序

给定一个正整数 n,使用 start 打印一个六边形图案,每边都包含 n 个星星。
例子 :

Input : 4
Output :   
   *
  * *
 *   *
*     *
*     *
*     *
*     *
 *   *
  * *
   *
// Hexagon pattern with each side
// having n stars where n is the user input.

Input : 6
Output :   
     *
    * *
   *   *
  *     *
 *       *
*         *
*         *
*         *
*         *
*         *
*         *
 *       *
  *     *
   *   *
    * *
     *

C++
// C++ program to print hexagonal pattern
#include 
 
void printHexagon(int n)
{
    int l = 2 * n - 1;
 
    /**** Print upper part *****/
    for (int i = 0; i < n; i++)
    {
        int elem = i + n;
 
        // loop for printing the
        // whitespaces and the star
        for (int k = 0; k < elem; k++)
        {
            // prints the star
            if ((k == n + i - 1) || (k == n - i - 1))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
 
    /**** Print middle part *****/
    // Here we print two vertical lines
    for (int m = 0; m < n - 2; m++)
    {
        for (int j = 0; j < l; j++)
        {
            if (j == 0 || j == l - 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
 
    /**** Print Lower part *****/
    int r = n - 1;
    for (int h = r; h >= 0; h--)
    {
        int elem = h + n;
        for (int k = 0; k < elem; k++)
        {
            if ((k == n + h - 1) || (k == n - h - 1))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
}
 
// Driver Code
int main()
{
    // length of a side
    // n >= 2
    int n = 3;
    printHexagon(n);
    return 0;
}


Java
// Java program to print hexagonal pattern
 
class GFG
{
    static void printHexagon(int n)
    {
        int l = 2 * n - 1;
     
        /**** Print upper part *****/
        for (int i = 0; i < n; i++)
        {
            int elem = i + n;
     
            // loop for printing the
            // whitespaces and the star
            for (int k = 0; k < elem; k++)
            {
                // prints the star
                if ((k == n + i - 1) ||
                    (k == n - i - 1))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
     
        /**** Print middle part *****/
        // Here we print two vertical lines
        for (int m = 0; m < n - 2; m++)
        {
            for (int j = 0; j < l; j++)
            {
                if (j == 0 || j == l - 1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
     
        /**** Print Lower part *****/
        int r = n - 1;
        for (int h = r; h >= 0; h--)
        {
            int elem = h + n;
            for (int k = 0; k < elem; k++)
            {
                if ((k == n + h - 1) ||
                    (k == n - h - 1))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        // length of a side
        // n >= 2
        int n = 3;
        printHexagon(n);
    }
}
 
// This code is contributed by Anant Agarwal.


Python 3
# Python3 program to
# print hexagonal pattern
def printHexagon(n):
 
    l = 2 * n - 1
 
    # Print upper part
    for i in range(0, n):
     
        elem = i + n
 
        # loop for printing the
        # whitespaces and the star
        for k in range(0, elem):
         
            # prints the star
            if ((k == n + i - 1) or
                (k == n - i - 1)):
                print("*", end = "")
            else:
                print(" ", end = "")
         
        print("")
     
    # Print middle part
    # Here we print two
    # vertical lines
    for m in range(0, n - 2):
     
        for j in range(0, l):
             
            if (j == 0 or j == l - 1):
                print("*", end = "")
            else:
                print(" ", end = "")
         
        print("")
     
    # Print Lower part
    r = n - 1
    for h in range(r, -1, -1):
     
        elem = h + n
        for k in range(0, elem):
         
            if ((k == n + h - 1) or
                (k == n - h - 1)):
                print("*", end = "")
            else:
                print(" ", end = "")
                 
        print("")
     
# Driver Code
# length of a side
# n >= 2
n = 3
printHexagon(n)
 
# This code is contributed
# by Smitha


C#
// C# program to print hexagonal pattern
using System;
class GFG
{
    static void printHexagon(int n)
    {
        int l = 2 * n - 1;
       
        /**** Print upper part *****/
        for (int i = 0; i < n; i++)
        {
            int elem = i + n;
       
            // loop for printing the
            // whitespaces and the star
            for (int k = 0; k < elem; k++)
            {
                // prints the star
                if ((k == n + i - 1) ||
                    (k == n - i - 1))
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
                    Console.WriteLine();
        }
       
        /**** Print middle part *****/
        // Here we print two vertical lines
        for (int m = 0; m < n - 2; m++)
        {
            for (int j = 0; j < l; j++)
            {
                if (j == 0 || j == l - 1)
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
                   Console.WriteLine();
        }
       
        /**** Print Lower part *****/
        int r = n - 1;
        for (int h = r; h >= 0; h--)
        {
            int elem = h + n;
            for (int k = 0; k < elem; k++)
            {
                if ((k == n + h - 1) ||
                    (k == n - h - 1))
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
                    Console.WriteLine();
        }
    }
       
    // Driver code
    public static void Main ()
    {
        // length of a side
        // n >= 2
        int n = 3;
        printHexagon(n);
    }
}
   
// This code is contributed by vt_m.


PHP
= 0; $h--)
    {
        $elem = $h + $n;
        for ($k = 0; $k < $elem; $k++)
        {
            if (($k == $n + $h - 1) ||
                ($k == $n - $h - 1))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
}
 
// Driver Code
 
// length of a side n >= 2
$n = 3;
printHexagon($n);
 
// This code is contributed by mits
?>


Javascript


输出 :

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