Program to print right and left arrow patterns

Write to program to print the right arrow pattern and the left arrow pattern formed of stars. Input is an odd number n which represents height and width of pattern to be printed.

Examples:

Input : n = 9
Output :
    *
     *               
      *  
       *
*********
       *
      *
     *
    *


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


Input : n = 7
Output :
   *
    *
     *
*******
     *
    *
   *


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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print Pyramid pattern
#include <iostream>
using namespace std;
  
// function to print right arrow pattern
void rightpattern(int n)
{
    // for printing upper portion
    int c1 = (n - 1) / 2;
  
    // for printing lower portion
    int c2 = 3 * n / 2 - 1;
  
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
  
            // checking conditions to print right arrow
            if (j - i == c1 || i + j == c2 || i == c1)
                cout << "*";
  
            // otherwise print space
            else
                cout << " ";
        }
        // for jumping to next line
        cout << "\n";
    }
}
  
// function to print left arrow pattern
void leftpattern(int n)
{
    char s = ' ';
    char st = '*';
  
    // for printing upper part
    for (int i = (n - 1) / 2; i > 0; i--)
        cout << string(i, s) << st << endl;
  
    // for printing middle part
    for (int i = 0; i < n; i++)
        cout << "*";
    cout << endl;
  
    // for printing lower part
    for (int i = 1; i <= (n - 1) / 2; i++)
        cout << string(i, s) << st << endl;
    cout << endl;
}
  
// Driver program
int main()
{
    int n = 9; // Must be odd
  
    // function calling to print right arrow
    rightpattern(n);
  
    cout << endl  << endl;
  
    // function calling to print left arrow
    leftpattern(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print Pyramid pattern 
class GFG 
{
  
// function to print right arrow pattern 
static void rightpattern(int n)
{
    // for printing upper portion 
    int c1 = (n - 1) / 2;
  
    // for printing lower portion 
    int c2 = 3 * n / 2 - 1;
  
    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < n; j++) 
        {
  
            // checking conditions to print
            // right arrow 
            if (j - i == c1 || 
                i + j == c2 || i == c1) 
            {
                System.out.print("*");
            } // otherwise print space 
            else 
            {
                System.out.print(" ");
            }
        }
        // for jumping to next line 
        System.out.print("\n");
    }
}
  
static void string(int n) 
{
    for (int i = n; i > 0; i--) 
    {
        System.out.print(" ");
    }
}
  
// function to print left arrow pattern 
static void leftpattern(int n) 
{
    char s = ' ';
    char st = '*';
  
    // for printing upper part 
    for (int i = (n - 1) / 2; i > 0; i--) 
    {
        string(i);
        System.out.println(st);
    }
      
    // for printing middle part 
    for (int i = 0; i < n; i++) 
    {
        System.out.print("*");
    }
    System.out.println();
  
    // for printing lower part 
    for (int i = 1; i <= (n - 1) / 2; i++) 
    {
        string(i);
        System.out.println(st);
    }
    System.out.println();
}
  
// Driver Code
public static void main(String args[])
{
    int n = 9; // Must be odd 
  
    // function calling to print 
    // right arrow 
    rightpattern(n);
  
    System.out.println("\n");
  
    // function calling to print 
    // left arrow 
    leftpattern(n);
}
}
  
// This code is contributed 
// by PrinciRaj1992 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print Pyramid pattern 
using System;
  
class GFG 
{
  
// function to print right arrow pattern 
static void rightpattern(int n)
{
    // for printing upper portion 
    int c1 = (n - 1) / 2;
  
    // for printing lower portion 
    int c2 = 3 * n / 2 - 1;
  
    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < n; j++) 
        {
  
            // checking conditions to print
            // right arrow 
            if (j - i == c1 || 
                i + j == c2 || i == c1) 
            {
                Console.Write("*");
            } // otherwise print space 
            else
            {
                Console.Write(" ");
            }
        }
        // for jumping to next line 
        Console.Write("\n");
    }
}
  
static void String(int n) 
{
    for (int i = n; i > 0; i--) 
    {
        Console.Write(" ");
    }
}
  
// function to print left arrow pattern 
static void leftpattern(int n) 
{
    char s = ' ';
    char st = '*';
  
    // for printing upper part 
    for (int i = (n - 1) / 2; i > 0; i--) 
    {
        String(i);
        Console.WriteLine(st);
    }
      
    // for printing middle part 
    for (int i = 0; i < n; i++) 
    {
        Console.Write("*");
    }
    Console.WriteLine();
  
    // for printing lower part 
    for (int i = 1; i <= (n - 1) / 2; i++) 
    {
        String(i);
        Console.WriteLine(st);
    }
    Console.WriteLine();
}
  
// Driver Code
public static void Main()
{
    int n = 9; // Must be odd 
  
    // function calling to print 
    // right arrow 
    rightpattern(n);
  
    Console.WriteLine("\n");
  
    // function calling to print 
    // left arrow 
    leftpattern(n);
}
}
  
// This code is contributed 
// by Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// function to print right arrow pattern
function rightpattern($n)
{
      
    // for printing upper portion
    $c1 = ($n - 1) / 2;
  
    // for printing lower portion
    $c2 = floor(3 * $n / 2 - 1);
  
    for ($i = 0; $i < $n; $i++)
    {
        for ($j = 0; $j < $n; $j++) 
        {
  
            // checking conditions to 
            // print right arrow
            if (($j - $i) == $c1 || 
                ($i + $j) == $c2 || 
                 $i == $c1)
                echo "*";
  
            // otherwise print space
            else
                echo " ";
        }
          
        // for jumping to next line
        echo "\n";
    }
}
  
// function to print left arrow pattern
function leftpattern($n)
{
    $s = ' ';
    $st = '*';
  
    // for printing upper part
    for ($i = ($n - 1) / 2; $i > 0; $i--)
    {
    for($j = 0; $j < $i; $j++)
        echo " ";
    echo $st."\n";
    }
      
    // for printing middle part
    for ($i = 0; $i < $n; $i++)
        echo "*";
    echo "\n";
  
    // for printing lower part
    for ($i = 1; $i <= ($n - 1) / 2; $i++)
    {
            for($j = 0; $j < $i; $j++)
        echo " ";
    echo $st."\n";
    }
    echo "\n";
}
  
    // Driver Code
    $n = 9; // Must be odd
  
    // function calling to 
    // print right arrow
    rightpattern($n);
  
    echo "\n\n";
  
    // function calling to 
    // print left arrow
    leftpattern($n);
  
  
// This code is contributed by mits 
  
?>

chevron_right



Output:


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


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

Time Complexity: O(n^2)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.