📜  打印图案“D”的程序

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

打印图案“D”的程序

在本文中,我们将学习如何使用星号和空格打印图案 D。给定一个数字 n,我们将编写一个程序在 n 行或行上打印模式 D。
例子 :

Input : n = 9
Output : 
 ******  
 *     * 
 *     * 
 *     * 
 *     * 
 *     * 
 *     * 
 *     * 
 ******  

Input : n = 12
Output :
 *********  
 *        * 
 *        * 
 *        * 
 *        * 
 *        * 
 *        * 
 *        * 
 *        * 
 *        * 
 *        * 
 *********  

如果我们尝试用(行,列)矩阵分析这张图片,绿色圆圈代表图案 D 中星星的位置,我们将学习这些步骤。在这里,我们按列执行操作。所以,对于第一行星星,我们设置了第一个 if 条件,其中列位置为 1 的位置获得星星。然后所有大于 1 且小于 (n-2) 的列以及等于 1 和 n-1 的行位置都获得星号。最后,所有值为 n-2 且行不等于 0 或 6 的列都获得星号,从而形成模式“D”。其他步骤是不言自明的,可以从图中的行和列的位置来理解。
注意:为“n”选择最小值 4 以获得“D”模式的可见演示。

C++
// C++ program to print the pattern "D"
#include 
using namespace std;
 
// Function to generate the pattern D
void D_Pattern(int n){
     
    // loop for rows
    for (int i = 0; i < n; i++){
         
        // loop for columns
        for (int j = 0; j <= n; j++){
             
            // Logic to generate stars for *
            if (j == 1 || ((i == 0 || i == n-1) &&
               (j > 1 && j < n-2)) || (j == n-2 &&
                i != 0 && i != n-1))
                cout<< "*";
                 
            // For the spaces
            else
                cout<< " ";
        }
         
    // For changing line
    cout<< endl;
    }
}
 
// Driver Code
int main()
{
  int n = 9;
   
  // Function calling
  D_Pattern(n);
  return 0;
}
 
// This article is contributed by mits


Java
// Java program to print the pattern "D"
import java.util.*;
 
class GFG {
 
    // Function to generate the pattern D
    static void D_Pattern(int n){
         
        // loop for rows
        for (int i = 0; i < n; i++){
             
            // loop for columns
            for (int j = 0; j <= n; j++){
                 
                // Logic to generate stars
                // for *
                if (j == 1 || ((i == 0 ||
                             i == n-1) &&
                   (j > 1 && j < n-2)) ||
                   (j == n-2 && i != 0 &&
                               i != n-1))
                    System.out.print("*");
                     
                // For the spaces
                else
                    System.out.print(" ");
            }
             
        // For changing line
        System.out.println();
        }
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int n = 9;
 
        // Function calling
        D_Pattern(n);
    }
}
 
// This code is contributed by ChitraNayal.


Python3
# Python program to print the pattern "D"
# Function to generate the pattern D
def D_Pattern(string, n):
     
    # loop for rows
    for i in range(0, n):   
         
        # loop for columns
        for j in range(0, n):
             
            # Logic to generate stars for *
            if (j == 1 or ((i == 0 or i == n-1) and
               (j > 1 and j < n-2)) or (j == n-2 and
                i != 0 and i != n-1)): 
                string = string + "*"   
             
            # For the spaces
            else:     
                string = string + " "
                 
        # For changing line
        string = string + "\n"   
    return(string);
 
# Driver Code
string = "";
n = 9
print(D_Pattern(string, n));


C#
// C# program to print
// the pattern "D"
using System;
 
class GFG
{
    // Function to generate
    // the pattern D
    static void D_Pattern(int n)
    {
         
        // loop for rows
        for (int i = 0; i < n; i++)
        {
             
            // loop for columns
            for (int j = 0; j <= n; j++)
            {
                 
                // Logic to generate
                // stars for *
                if (j == 1 || ((i == 0 ||
                           i == n - 1) &&
                 (j > 1 && j < n - 2)) ||
                 (j == n - 2 && i != 0 &&
                          i != n - 1))
                    Console.Write("*");
                     
                // For the spaces
                else
                    Console.Write(" ");
            }
             
        // For changing line
        Console.WriteLine();
        }
    }
     
    // Driver Code
    static public void Main ()
    {
        int n = 9;
         
        // Function calling
        D_Pattern(n);
    }
}
 
// This code is contributed by ajit


PHP
 1 and $j < $n-2)) or ($j == $n-2 and
                $i != 0 and $i != $n-1))
                echo "*";
                 
            # For the spaces
            else 
                echo " ";    
        }
         
    # For changing line
    echo "\n";
    }
}
 
// Driver Code
$n = 9;
D_Pattern($n)
?>


Javascript


输出 :

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