📜  程序打印空心三角形图案

📅  最后修改于: 2021-06-01 01:40:47             🧑  作者: Mango

给定线数为N,任务是形成给定的空心三角形图案。
例子:

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

方法:

  1. 输入要从用户打印的行数为N。
  2. 要遍历行,请从行数开始运行一个外循环,直到其大于1。循环结构应类似于for(i = N; i> = 1; i–)。
  3. 要打印空格,请运行从i到空格(另一个局部变量)的内部循环。循环结构应类似于for(k = 1; k = 1; j–);在此循环中打印星星。
  4. 打印一行的所有列后,移至下一行,即打印新行。

下面是上述方法的实现:

C++
// C++ program for printing
// the hollow triangle pattern
 
#include 
using namespace std;
 
// Function for printing pattern
void pattern(int N)
{
    int i, j, k = 0, space = 1, rows = N;
 
    // For printing stars
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            cout << "*";
        }
        if (i != rows) {
            // for printing space
            for (k = 1; k <= space; k++) {
                cout << " ";
            }
 
            // increment by 2
            space = space + 2;
        }
        for (j = i; j >= 1; j--) {
            if (j != rows)
                cout << "*";
        }
        cout << "\n";
    }
    cout << "\n";
}
 
// Driver code
int main()
{
 
    // Get N
    int N = 6;
 
    // Print the pattern
    pattern(N);
 
    return 0;
}


C
// C program for printing
// the hollow triangle pattern
 
#include 
 
// Function for printing pattern
void pattern(int N)
{
    int i, j, k = 0, space = 1, rows = N;
 
    // For printing stars
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        if (i != rows) {
            // for printing space
            for (k = 1; k <= space; k++) {
                printf(" ");
            }
 
            // increment by 2
            space = space + 2;
        }
        for (j = i; j >= 1; j--) {
            if (j != rows)
                printf("*");
        }
        printf("\n");
    }
    printf("\n");
}
 
// Driver code
int main()
{
 
    // Get N
    int N = 6;
 
    // Print the pattern
    pattern(N);
 
    return 0;
}


Java
// Java program for printing
// the hollow triangle pattern
import java.util.*;
 
class solution
{
 
// Function for printing pattern
static void pattern(int N)
{
    int i, j, k = 0, space = 1, rows = N;
 
    // For printing stars
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            System.out.print("*");
        }
        if (i != rows) {
            // for printing space
            for (k = 1; k <= space; k++) {
            System.out.print(" ");
            }
 
            // increment by 2
            space = space + 2;
        }
        for (j = i; j >= 1; j--) {
            if (j != rows)
                System.out.print("*");
        }
        System.out.print("\n");
    }
    System.out.print("\n");
}
 
// Driver code
public static void main(String args[])
{
 
    // Get N
    int N = 6;
 
    // Print the pattern
    pattern(N);
 
}
}
 
//This code is contributed by Surendra_Gangwar


Python3
# Python 3 program for printing
# the hollow triangle pattern
 
# Function for printing pattern
def pattern(N):
    k, space, rows = 0, 1, N
 
    # For printing stars
    for i in range(rows, 0, -1):
        for j in range(1, i + 1):
            print('*', end = '')
 
        if i != rows:
            # for printing space
            for k in range(1, space + 1):
                print(' ', end = '')
 
            # increment by 2
            space += 2
        for j in range(i, 0, -1):
            if j != rows:
                print('*', end = '')
        print()
    print()
 
# Driver Code
 
# Get N
N = 6
 
# Print the pattern
pattern(N)
 
# This code is contributed by
# SamyuktaSHegde


C#
// C# program for printing
// the hollow triangle pattern
using System;
 
class GFG
{
 
// Function for printing pattern
static void pattern(int N)
{
    int i, j, k = 0, space = 1, rows = N;
 
    // For printing stars
    for (i = rows; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
        {
            Console.WriteLine("*");
        }
        if (i != rows)
        {
            // for printing space
            for (k = 1; k <= space; k++)
            {
            Console.Write(" ");
            }
 
            // increment by 2
            space = space + 2;
        }
        for (j = i; j >= 1; j--)
        {
            if (j != rows)
                Console.Write("*");
        }
        Console.Write("\n");
    }
    Console.Write("\n");
}
 
// Driver code
public static void Main()
{
 
    // Get N
    int N = 6;
 
    // Print the pattern
    pattern(N);
}
}
 
// This code is contributed
// by Rajput-Ji


PHP
= 1; $i--)
    {
        for ($j = 1; $j <= $i; $j++)
        {
            echo "*";
        }
        if ($i != $rows)
        {
            // for printing space
            for ($k = 1; $k <= $space; $k++)
            {
                echo " ";
            }
 
            // increment by 2
            $space = $space + 2;
        }
        for ($j = $i; $j >= 1; $j--)
        {
            if ($j != $rows)
                echo "*";
        }
        echo "\n";
    }
    echo "\n";
}
 
// Driver code
 
// Get N
$N = 6;
 
// Print the pattern
pattern($N);
 
// This code is contributed by
// Archana_kumari
?>


Javascript


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

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