Program to print the arrow pattern

Given the value of n, print the arrow pattern.

Examples :

Input : n = 5
Output : 

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

Input : n = 7
Output : 

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

Below is the program to print the arrow pattern:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the
// arrow pattern
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to print 
// the arrow pattern
  
// Function to print pattern
function print_arrow($n)
{
    // for printing upper part
    // of the arrow
    for ($i = 1; $i < $n; $i++) 
    {
  
        // To give space before printing
        // stars in upper part of arrow
        for ($j = 0; $j < $i; $j++)
        {
            echo " ";
        }
  
        // To print stars in upper
        // part of the arrow
        for ($k = 0; $k < $i; $k++)
        {
            echo "*";
        }
        echo "\n";
    }
  
    // for printing lower part
    // of the arrow
    for ($i = 0; $i < $n; $i++) 
    {
  
        // To give space before printing
        // stars in lower part of arrow
        for ($j = 0; $j < $n - $i; $j++) 
        {
            echo " ";
        }
  
        // To print stars in lower
        // part of the arrow
        for ($k = 0; $k < $n - $i; $k++) 
        {
            echo "*";
        }
        echo "\n";
    }
}
  
// Driver code
$n = 5;
print_arrow($n);
  
// This code is contributed by Mithun Kumar
?>

chevron_right



Output:


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

Another method:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the
// arrow pattern
#include <iostream>
#include<stdlib.h>
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;
}

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to print the
// arrow pattern
#include <stdio.h>
#include<stdlib.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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. 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right



Output:


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


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.