📜  打印箭头图案的程序

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

打印箭头图案的程序

给定 n 的值,打印箭头图案。
例子 :

Input : n = 5
Output : 

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

Input : n = 7
Output : 

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

下面是打印箭头图案的程序:

C++
// C++ program to print the
// arrow pattern
#include 
using namespace std;
 
// Function to print pattern
void print_arrow(int n)
{
    // for printing upper part
    // of the arrow
    for (int i = 1; i < n; i++) {
 
        // To give space before printing
        // stars in upper part of arrow
        for (int j = 0; j < i; j++) {
            cout << " ";
        }
 
        // To print stars in upper
        // part of the arrow
        for (int k = 0; k < i; k++) {
            cout << "*";
        }
        cout << "\n";
    }
 
    // for printing lower part
    // of the arrow
    for (int i = 0; i < n; i++) {
 
        // To give space before printing
        // stars in lower part of arrow
        for (int j = 0; j < n - i; j++) {
            cout << " ";
        }
 
        // To print stars in lower
        // part of the arrow
        for (int k = 0; k < n - i; k++) {
            cout << "*";
        }
        cout << "\n";
    }
}
 
// Driver code
int main()
{
    // taking input from user
    int n = 5;
 
    // function calling
    print_arrow(n);
    return 0;
}


Java
// Java program to print the
// arrow pattern
 
import java.io.*;
 
class GFG {
 
// Function to print pattern
static void print_arrow(int n)
{
    // for printing upper part
    // of the arrow
    for (int i = 1; i < n; i++) {
  
        // To give space before printing
        // stars in upper part of arrow
        for (int j = 0; j < i; j++) {
            System.out.print(" ");
        }
  
        // To print stars in upper
        // part of the arrow
        for (int k = 0; k < i; k++) {
            System.out.print("*");
        }
        System.out.println();
    }
  
    // for printing lower part
    // of the arrow
    for (int i = 0; i < n; i++) {
  
        // To give space before printing
        // stars in lower part of arrow
        for (int j = 0; j < n - i; j++) {
            System.out.print(" ");
        }
  
        // To print stars in lower
        // part of the arrow
        for (int k = 0; k < n - i; k++) {
            System.out.print("*");
        }
        System.out.println();
    }
}
 
public static void main (String[] args)
       {
 
    // taking input from user
    int n = 5;
  
    // function calling
    print_arrow(n);
         
    }
}
 
// This code is contributed by Gitanjali.


Python3
# Python program to print the
# arrow pattern
 
import math
 
# Function to print pattern
def print_arrow(n):
 
    # for printing upper part
    # of the arrow
    for i in range(1,n):
  
        # To give space before printing
        # stars in upper part of arrow
        for j in range(0,i):
            print(" ",end="")
         
  
        # To print stars in upper
        # part of the arrow
        for k in range(0,i):
            print("*",end="")
         
        print()
     
  
    # for printing lower part
    # of the arrow
    for i in range(0,n):
  
        # To give space before printing
        # stars in lower part of arrow
        for j in range (0,n-i):
            print(" ",end="")
         
  
        # To print stars in lower
        # part of the arrow
        for k in range (0,n-i):
            print("*",end="")
         
        print()
     
#driver code
 
n = 5
  
# function calling
print_arrow(n)
 
# This code is contributed by Gitanjali.


C#
// C# program to print
// the arrow pattern
using System;
 
class GFG {
 
    // Function to print pattern
    static void print_arrow(int n)
    {
        // for printing upper
        // part of the arrow
        for (int i = 1; i < n; i++) {
 
            // To give space before printing
            // stars in upper part of arrow
            for (int j = 0; j < i; j++) {
                Console.Write(" ");
            }
 
            // To print stars in upper
            // part of the arrow
            for (int k = 0; k < i; k++) {
                Console.Write("*");
            }
            Console.WriteLine();
        }
 
        // for printing lower
        // part of the arrow
        for (int i = 0; i < n; i++) {
 
            // To give space before printing
            // stars in lower part of arrow
            for (int j = 0; j < n - i; j++) {
                Console.Write(" ");
            }
 
            // To print stars in lower
            // part of the arrow
            for (int k = 0; k < n - i; k++) {
                Console.Write("*");
            }
            Console.WriteLine();
        }
    }
     
    // Driver code
    public static void Main()
    {
        // taking input from user
        int n = 5;
 
        // function calling
        print_arrow(n);
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ program to print the
// arrow pattern
#include 
#include
using namespace std;
class gfg
{
public : void print_arrow(int n)
{
    // Total no of rows
    for(int i = 0; i < n + n - 1; i++)
    {
        int size = i;
        if(i >= n)
        {
            size = n + n - i - 2;
        }
         
        // cols in each row
        for(int j = 0; j <= size + size; j++)    
            {
            if(j < size)     // For printing space
            {
                cout<<" ";
            }
            else         // For printing *
            {
                cout<<"*";
            }
        }
        cout<<"\n";
    }
 }
};
 
// Driver code
int main()
{
    gfg g;
    // taking input from user
    int n = 5;
 
    // function calling
    g.print_arrow(n);
    return 0;
}


C
// C program to print the
// arrow pattern
#include 
#include
void print_arrow(int n)
{
    // Total no of rows
    for(int i = 0; i < n + n - 1; i++)
    {
        int size = i;
        if(i >= n)
        {
            size = n + n - i - 2;
        }
         
        // cols in each row
        for(int j = 0; j <= size + size; j++)    
            {
            if(j < size)     // For printing space
            {
                printf(" ");
            }
            else         // For printing *
            {
                printf("*");
            }
        }
        printf("\n");
    }
}
 
// Driver code
int main()
{
    // taking input from user
    int n = 5;
 
    // function calling
    print_arrow(n);
    return 0;
}


Java
// Java program to print the
// arrow pattern
class GFG
{
     
static void print_arrow(int n)
{
    // Total no of rows
    for(int i = 0; i < n + n - 1; i++)
    {
        int size = i;
        if(i >= n)
        {
            size = n + n - i - 2;
        }
         
        // cols in each row
        for(int j = 0; j <= size + size; j++)    
            {
            if(j < size)     // For printing space
            {
                System.out.print(" ");
            }
            else         // For printing *
            {
                System.out.print("*");
            }
        }
        System.out.print("\n");
    }
}
 
// Driver code
public static void main(String args[])
{
    // taking input from user
    int n = 5;
 
    // function calling
    print_arrow(n);
}
}
 
// This code is contributed
// by Akanksha Rai


Python3
# Python 3 program to print the
# arrow pattern
def print_arrow(n):
 
    # Total no of rows
    for i in range(n + n - 1):
        size = i
        if i >= n:
            size = n + n - i - 2
 
        # cols in each row
        for j in range(size + size):
            if j < size:     # For printing space
                print(' ', end = '')
            else:         # For printing *
                print('*', end = '')
        print()
 
# Driver code
 
# taking input from user
n = 5
 
# function calling
print_arrow(n)
 
# This code is contributed
# by SamyuktaSHegde.


C#
// C# program to print the
// arrow pattern
using System;
class GFG
{
     
static void print_arrow(int n)
{
    // Total no of rows
    for(int i = 0; i < n + n - 1; i++)
    {
        int size = i;
        if(i >= n)
        {
            size = n + n - i - 2;
        }
         
        // cols in each row
        for(int j = 0; j <= size + size; j++)    
            {
            if(j < size)     // For printing space
            {
                Console.Write(" ");
            }
            else         // For printing *
            {
                Console.Write("*");
            }
        }
        Console.Write("\n");
    }
}
 
// Driver code
public static void Main()
{
    // taking input from user
    int n = 5;
 
    // function calling
    print_arrow(n);
}
}
 
// This code is contributed
// by Akanksha Rai


Javascript


输出:

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

另一种方法:

C++

// C++ program to print the
// arrow pattern
#include 
#include
using namespace std;
class gfg
{
public : void print_arrow(int n)
{
    // Total no of rows
    for(int i = 0; i < n + n - 1; i++)
    {
        int size = i;
        if(i >= n)
        {
            size = n + n - i - 2;
        }
         
        // cols in each row
        for(int j = 0; j <= size + size; j++)    
            {
            if(j < size)     // For printing space
            {
                cout<<" ";
            }
            else         // For printing *
            {
                cout<<"*";
            }
        }
        cout<<"\n";
    }
 }
};
 
// Driver code
int main()
{
    gfg g;
    // taking input from user
    int n = 5;
 
    // function calling
    g.print_arrow(n);
    return 0;
}

C

// C program to print the
// arrow pattern
#include 
#include
void print_arrow(int n)
{
    // Total no of rows
    for(int i = 0; i < n + n - 1; i++)
    {
        int size = i;
        if(i >= n)
        {
            size = n + n - i - 2;
        }
         
        // cols in each row
        for(int j = 0; j <= size + size; j++)    
            {
            if(j < size)     // For printing space
            {
                printf(" ");
            }
            else         // For printing *
            {
                printf("*");
            }
        }
        printf("\n");
    }
}
 
// Driver code
int main()
{
    // taking input from user
    int n = 5;
 
    // function calling
    print_arrow(n);
    return 0;
}

Java

// Java program to print the
// arrow pattern
class GFG
{
     
static void print_arrow(int n)
{
    // Total no of rows
    for(int i = 0; i < n + n - 1; i++)
    {
        int size = i;
        if(i >= n)
        {
            size = n + n - i - 2;
        }
         
        // cols in each row
        for(int j = 0; j <= size + size; j++)    
            {
            if(j < size)     // For printing space
            {
                System.out.print(" ");
            }
            else         // For printing *
            {
                System.out.print("*");
            }
        }
        System.out.print("\n");
    }
}
 
// Driver code
public static void main(String args[])
{
    // taking input from user
    int n = 5;
 
    // function calling
    print_arrow(n);
}
}
 
// This code is contributed
// by Akanksha Rai

Python3

# Python 3 program to print the
# arrow pattern
def print_arrow(n):
 
    # Total no of rows
    for i in range(n + n - 1):
        size = i
        if i >= n:
            size = n + n - i - 2
 
        # cols in each row
        for j in range(size + size):
            if j < size:     # For printing space
                print(' ', end = '')
            else:         # For printing *
                print('*', end = '')
        print()
 
# Driver code
 
# taking input from user
n = 5
 
# function calling
print_arrow(n)
 
# This code is contributed
# by SamyuktaSHegde.

C#

// C# program to print the
// arrow pattern
using System;
class GFG
{
     
static void print_arrow(int n)
{
    // Total no of rows
    for(int i = 0; i < n + n - 1; i++)
    {
        int size = i;
        if(i >= n)
        {
            size = n + n - i - 2;
        }
         
        // cols in each row
        for(int j = 0; j <= size + size; j++)    
            {
            if(j < size)     // For printing space
            {
                Console.Write(" ");
            }
            else         // For printing *
            {
                Console.Write("*");
            }
        }
        Console.Write("\n");
    }
}
 
// Driver code
public static void Main()
{
    // taking input from user
    int n = 5;
 
    // function calling
    print_arrow(n);
}
}
 
// This code is contributed
// by Akanksha Rai

Javascript


输出:

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