Program to print the arrow pattern

29 Oct, 2021
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++
Java
Python3
C#
PHP
Javascript
// C++ program to print the// arrow pattern#include <bits/stdc++.h>using namespace std; // Function to print patternvoid 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 codeint main(){    // taking input from user    int n = 5;     // function calling    print_arrow(n);    return 0;}
// Java program to print the// arrow pattern import java.io.*; class GFG { // Function to print patternstatic 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.
# Python program to print the# arrow pattern import math # Function to print patterndef 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 callingprint_arrow(n) # This code is contributed by Gitanjali.
// C# program to print// the arrow patternusing 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// PHP implementation to print// the arrow pattern // Function to print patternfunction 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?>
<script>       // JavaScript program to print the      // arrow pattern       // Function to print pattern      function print_arrow(n)      {        // for printing upper part        // of the arrow        for (var i = 1; i < n; i++)        {          // To give space before printing          // stars in upper part of arrow          for (var j = 0; j < i; j++)          {            document.write("  ");          }           // To print stars in upper          // part of the arrow          for (var k = 0; k < i; k++) {            document.write("*");          }          document.write("<br>");        }         // for printing lower part        // of the arrow        for (var i = 0; i < n; i++)        {          // To give space before printing          // stars in lower part of arrow          for (var j = 0; j < n - i; j++)          {            document.write("  ");          }           // To print stars in lower          // part of the arrow          for (var k = 0; k < n - i; k++)          {            document.write("*");          }          document.write("<br>");        }      }       // Driver code             // taking input from user      var n = 5;       // function calling      print_arrow(n);       </script>
Output:  
 *
  **
   ***
    ****
     *****
    ****
   ***
  **
 *
Another method:  
C++
C
Java
Python3
C#
Javascript
// 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 codeint main(){    gfg g;    // taking input from user    int n = 5;     // function calling    g.print_arrow(n);    return 0;}
// 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 codeint main(){    // taking input from user    int n = 5;     // function calling    print_arrow(n);    return 0;}
// Java program to print the// arrow patternclass 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 codepublic static void main(String args[]){    // taking input from user    int n = 5;     // function calling    print_arrow(n);}} // This code is contributed// by Akanksha Rai
# Python 3 program to print the# arrow patterndef 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 usern = 5 # function callingprint_arrow(n) # This code is contributed# by SamyuktaSHegde.
// C# program to print the// arrow patternusing 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 codepublic static void Main(){    // taking input from user    int n = 5;     // function calling    print_arrow(n);}} // This code is contributed// by Akanksha Rai
<script>// javascript program to print the// arrow patternfunction print_arrow(n){    // Total no of rows    for(var i = 0; i < n + n - 1; i++)    {        var size = i;        if(i >= n)        {            size = n + n - i - 2;        }                 // cols in each row        for(var j = 0; j <= size + size; j++)                {            if(j < size)     // For printing space            {                document.write(" ");            }            else         // For printing *            {                document.write("*");            }        }        document.write("<br>");    }} // Driver code // taking input from uservar n = 5; // function callingprint_arrow(n); // This code is contributed by Princi Singh</script>
Output:  
 *
  **
   ***
    ****
     *****
    ****
   ***
  **
 *
 
Mithun Kumar
dilsicrazz
SamyuktaSHegde
Akanksha_Rai
SoumikMondal
rdtank
princi singh
ankita_saini
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction To PYTHON
Interfaces in Java
C++ Classes and Objects
C++ Data Types
Operator Overloading in C++
Polymorphism in C++
Types of Operating Systems
Constructors in C++
Constructors in Java
Exceptions in Java