📜  打印梯形图的程序

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

打印梯形图的程序

给定一个整数N ,任务是使用“*”打印具有N步的梯子。梯子将在两侧栏杆之间留出 3 个空间。

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

Input: N = 4
Output: 
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *

方法:将模式分成两个子模式。

*    *
*    *
  • 将打印 N+1 次
*****
*    *
*    *
  • 这将被打印 N 次。

下面是上述方法的实现:

C++
// C++ program to print the ladder pattern
 
#include 
using namespace std;
 
// Function to print the desired ladder Pattern
void ladder_pattern(int N)
{
 
    for (int i = 0; i <= N; i++) {
        // Printing the sub-pattern 1
        // N+1 times
        cout << "*   *" << endl;
        cout << "*   *" << endl;
 
        if (i < N) {
            // Printing the sub-pattern 2
            // N times
            cout << "*****" << endl;
        }
    }
}
 
// Driver Code
int main()
{
    // Size of the Pattern
    int N = 3;
 
    // Print the ladder
    ladder_pattern(N);
 
    return 0;
}


Java
// Java program to print the ladder pattern
class GFG
{
 
// Function to print the desired ladder Pattern
static void ladder_pattern(int N)
{
 
    for (int i = 0; i <= N; i++)
    {
         
        // Printing the sub-pattern 1
        // N+1 times
        System.out.println("*   *");
        System.out.println("*   *");
 
        if (i < N)
        {
            // Printing the sub-pattern 2
            // N times
            System.out.println("*****" );
        }
    }
}
 
// Driver Code
public static void main(String args[])
{
    // Size of the Pattern
    int N = 3;
 
    // Print the ladder
    ladder_pattern(N);
}
}
 
// This code is contributed by Rajput Ji


Python3
# Python3 program to print the ladder pattern
 
# Function to print the desired ladder Pattern
def ladder_pattern(N) :
 
    for i in range(N + 1) :
         
        # Printing the sub-pattern 1
        # N + 1 times
        print("*   *");
        print("*   *");
 
        if (i < N) :
             
            # Printing the sub-pattern 2
            # N times
            print("*****");
 
# Driver Code
if __name__ == "__main__" :
     
    # Size of the Pattern
    N = 3;
 
    # Print the ladder
    ladder_pattern(N);
     
# This code is contributed by AnkitRai01


C#
// C# program to print the ladder pattern
using System;
 
class GFG
{
     
// Function to print the desired ladder Pattern
static void ladder_pattern(int N)
{
    for (int i = 0; i <= N; i++)
    {
         
        // Printing the sub-pattern 1
        // N+1 times
        Console.WriteLine("*   *");
        Console.WriteLine("*   *");
 
        if (i < N)
        {
            // Printing the sub-pattern 2
            // N times
            Console.WriteLine("*****");
        }
    }
}
 
// Driver Code
static public void Main ()
{
    // Size of the Pattern
    int N = 3;
 
    // Print the ladder
    ladder_pattern(N);
}
}
 
// This code is contributed by ajit.


Javascript


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