Mô hình số 2 kim tự tháp rỗng đầy đủ trong python

Mô hình số 2 kim tự tháp rỗng đầy đủ trong python

Các chương trình in đầy đủ mô hình kim tự tháp rỗng đã được cung cấp tại đây



Nội dung trang





1. Chương trình và đầu ra để in mẫu kim tự tháp đầy đủ




1. 1. Chương trình C & đầu ra để in toàn bộ mô hình kim tự tháp rỗng

C

Mã đã được sao chép

/******************************
      alphabetacoder.com
C program to print the full 
hollow pyramid pattern using *
*******************************/

#include 

int main() {
    // declare variables
    int row, i, j;

    // take input
    printf("Enter the number of rows: ");
    scanf("%d", & row);

    // new line
    printf("\n");

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print spaces before starting
        // position of each row 
        for (j = 1; j <= row - i; j++) {
            printf(" ");
        }

        // print *
        for (j = 1; j <= (2 * i - 1); j++) {
            // except for the last row
            if (i < row) {
                if (j == 1 || j == (2 * i - 1)) {
                    printf("*");
                } else {
                    printf(" ");
                }
            }
            // for the last row
            else {
                printf("*");
            }
        }
        // new line
        printf("\n");
    }

    return 0;
}

đầu ra


Nhập số hàng. 6


*

* *

*   *

*     *

*       *

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




1. 2. Chương trình C++ và đầu ra để in toàn bộ mô hình kim tự tháp rỗng

C++

Mã đã được sao chép

/******************************
      alphabetacoder.com
C++ program to print the full 
hollow pyramid pattern using *
*******************************/

#include 

using namespace std;

int main() {
    // declare variables
    int row, i, j;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // print spaces before starting
        // position of each row 
        for (j = 1; j <= row - i; j++) {
            cout << " ";
        }

        // print *
        for (j = 1; j <= (2 * i - 1); j++) {
            // except for the last row
            if (i < row) {
                if (j == 1 || j == (2 * i - 1)) {
                    cout << "*";
                } else {
                    cout << " ";
                }
            }
            // for the last row
            else {
                cout << "*";
            }
        }
        // new line
        cout << endl;
    }

    return 0;
}

đầu ra


Nhập số hàng. 4


*

* *

*   *

*******




1. 3. Chương trình Java và đầu ra để in toàn bộ mô hình kim tự tháp rỗng

Java

Mã đã được sao chép

/******************************
      alphabetacoder.com
Java program to print the full 
hollow pyramid pattern using *
*******************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);
        // declare variables
        int row, i, j;

        // take input
        System.out.print("Enter the number of rows: ");
        row = sc.nextInt();

        // new line
        System.out.println("");

        // display the pattern
        for (i = 1; i <= row; i++) {
            // print spaces before starting
            // position of each row 
            for (j = 1; j <= row - i; j++) {
                System.out.print(" ");
            }

            // print *
            for (j = 1; j <= (2 * i - 1); j++) {
                // except for the last row
                if (i < row) {
                    if (j == 1 || j == (2 * i - 1)) {
                        System.out.print("*");
                    } else {
                        System.out.print(" ");
                    }
                }
                // for the last row
                else {
                    System.out.print("*");
                }
            }
            // new line
            System.out.println("");
        }
    }
}

đầu ra


Nhập số hàng. 5


*

* *

*   *

*     *

*********




1. 4. Chương trình Python và đầu ra để in toàn bộ mô hình kim tự tháp rỗng

con trăn

Mã đã được sao chép

#********************************
#      alphabetacoder.com
#Python program to print the full 
#hollow pyramid pattern using *
#*********************************

# take input
row = int(input("Enter the number of rows: "))

# new line
print("")

# display the pattern
for i in range(1, row + 1):
    # print spaces before starting
    # position of each row 
    for j in range(1, row - i + 1):
        print(" ", end = "")
   
    # print *
    for j in range(1, 2 * i):
        # except for the last row
        if i < row:
            if j == 1 or j == (2 * i - 1):
                print("*", end = "")
            else:
                print(" ", end = "")
        # for the last row
        else:
            print("*", end = "")
    # new line
    print("")

đầu ra


Nhập số hàng. số 8


*

* *

*   *

*     *

*       *

**         *

*  *

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




1. 5. Chương trình C# và đầu ra để in toàn bộ mô hình kim tự tháp rỗng

C#

Mã đã được sao chép

/******************************
      alphabetacoder.com
C# program to print the full 
hollow pyramid pattern using *
*******************************/

using System;

namespace HollowPyramid {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int row, i, j;

            // take input
            Console.Write("Enter the number of rows: ");
            row = Convert.ToInt32(Console.ReadLine());

            // new line
            Console.WriteLine("");

            // display the pattern
            for (i = 1; i <= row; i++) {
                // print spaces before starting
                // position of each row 
                for (j = 1; j <= row - i; j++) {
                    Console.Write(" ");
                }

                // print *
                for (j = 1; j <= (2 * i - 1); j++) {
                    // except for the last row
                    if (i < row) {
                        if (j == 1 || j == (2 * i - 1)) {
                            Console.Write("*");
                        } else {
                            Console.Write(" ");
                        }
                    }
                    // for the last row
                    else {
                        Console.Write("*");
                    }
                }
                // new line
                Console.WriteLine("");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

đầu ra


Nhập số hàng. 7


*

* *

*   *

*     *

*       *

**         *

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




Làm cách nào để in mô hình ngôi sao kim tự tháp ngược rỗng bằng Python?

Mô hình ngôi sao tam giác vuông ngược rỗng trong python .
Bắt đầu
Lấy số hàng làm đầu vào từ người dùng và lưu nó vào num
Chạy vòng lặp 'i' số lần để lặp qua tất cả các hàng Bắt đầu từ i=0 đến num
Chạy một vòng lặp lồng nhau bên trong vòng lặp chính để in khoảng trắng bắt đầu từ j=0 đến i

Làm cách nào để tạo mẫu trong Python?

Các mẫu có thể được in bằng python bằng cách sử dụng các vòng lặp for đơn giản. Vòng lặp bên ngoài đầu tiên được sử dụng để xử lý số hàng và vòng lặp lồng bên trong được sử dụng để xử lý số lượng cột. Thao tác với các câu lệnh in, có thể in các mẫu số, mẫu bảng chữ cái hoặc mẫu sao khác nhau