Programa para imprimir el patrón de flechas

Dado el valor de n, imprima el patrón de flecha.
Ejemplos: 
 

Input : n = 5
Output : 

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

Input : n = 7
Output : 

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

A continuación se muestra el programa para imprimir el patrón de flecha:
 

C++

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

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

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

Javascript

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

Producción: 
 

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

Complejidad temporal: O(n 2 )

Espacio Auxiliar: O(1)
Otro método: 
 

C++

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

C

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

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

<script>
// javascript program to print the
// arrow pattern
function 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 user
var n = 5;
 
// function calling
print_arrow(n);
 
// This code is contributed by Princi Singh
</script>

Producción: 
 

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

Complejidad del tiempo: O(N 2 )

Espacio Auxiliar: O(1)
 

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *