Programa para patrón de estrella de flecha

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

Input : 5
Output :
    *****
   ****
  ***
 **
*
 **
  ***
   ****
    *****

Input : 7
Output : 
      *******
     ******
    *****
   ****
  ***
 **
*
 **
  ***
   ****
    *****
     ******
      *******

A continuación se muestra la implementación para imprimir el patrón anterior: 
 

C++

// C++ Implementation to print the pattern
 
#include <bits/stdc++.h>
using namespace std;
 
// arrow function
int arrow(int n)
{
    // Prints the upper part of the arrow
    for (int i = 1; i <= n; i++)
    {
        // for the spacing to form
        // the point of the arrow
        for (int j = i; j < n; j++)
        {
            printf(" ");
        }
         
        // for printing the star(*)
        for (int j = i; j <= n; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
     
    // Prints lower part of the arrow
    for (int i = 2; i <= n; i++)
    {
        // for the spacing to form
        // the point of the arrow
        for (int j = 1; j < i; j++)
        {
            printf(" ");
        }
         
        // for printing the star(*)
        for (int j = 1; j <= i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
}
 
// driver code
int main()
{
    // get the value from user
    int n = 5;
     
    // function calling
    arrow(n);
 
    return 0;
}

Java

// Java Implementation to
// print the above pattern
import java.io.*;
 
class GFG {
 
// arrow function
static void arrow(int n)
{
    // Prints the upper part of the arrow
    for (int i = 1; i <= n; i++)
    {
        // for the spacing to form
        // the point of the arrow
        for (int j = i; j < n; j++)
        {
            System.out.print(" ");
        }
         
        // for printing the star(*)
        for (int j = i; j <= n; j++)
        {
            System.out.print("*");
        }
             
    System.out.println();
    }
     
    // Prints lower part of the arrow
    for (int i = 2; i <= n; i++)
    {
        // for the spacing to form
        // the point of the arrow
        for (int j = 1; j < i; j++)
        {
            System.out.print(" ");
        }
         
        // for printing the star(*)
        for (int j = 1; j <= i; j++)
        {
            System.out.print("*");
        }
    System.out.print('\n');
    }
}
 
// driver code
public static void main(String[] Argv) {
     
    // get the value from user
    int n = 5;
     
    // function calling
    arrow(n);
}
}
 
// this code is contributed by 'vt_m'

Python3

# Python Implementation to
# print the pattern
  
# arrow function
def arrow(n):
 
    # Prints the upper part of the arrow
    for i in range(1, n+1):
     
        # for the spacing to form
        # the point of the arrow
        for j in range(i, n):
         
            print(" ", end="")
         
        # for printing the star(*)
        for j in range(i, n+1):
         
            print("*", end="")
         
        print()
      
    # Prints lower part of the arrow
    for i in range(2, n+1):
     
        # for the spacing to form
        # the point of the arrow
        for j in range(1, i):
         
            print(" ", end="")
         
        # for printing the star(*)
        for j in range(1, i+1):
         
            print("*", end="")
         
        print()
  
# driver code
# get the value from the user
n = 5
      
# function calling
arrow(n)
 
# This code is contributed
# by Anant Agarwal.

C#

// C# Implementation to
// print the above pattern
using System;
 
class GFG {
 
// arrow function
static void arrow(int n)
{
    // Prints the upper part of the arrow
    for (int i = 1; i <= n; i++)
    {
        // for the spacing to form
        // the point of the arrow
        for (int j = i; j < n; j++)
        {
            Console.Write(" ");
        }
         
        // for printing the star(*)
        for (int j = i; j <= n; j++)
        {
            Console.Write("*");
        }
             
           Console.WriteLine();
    }
     
    // Prints lower part of the arrow
    for (int i = 2; i <= n; i++)
    {
        // for the spacing to form
        // the point of the arrow
        for (int j = 1; j < i; j++)
        {
            Console.Write(" ");
        }
         
        // for printing the star(*)
        for (int j = 1; j <= i; j++)
        {
            Console.Write("*");
        }
         Console.WriteLine();
    }
}
 
// driver code
public static void Main() {
     
    // get the value from user
    int n = 5;
     
    // function calling
    arrow(n);
}
}
 
// this code is contributed by 'vt_m'

PHP

<?php
// PHP Implementation to print
// the pattern
 
function arrow($n)
{
    // Prints the upper part of
    // the arrow
    for ($i = 1; $i <= $n; $i++)
    {
        // for the spacing to form
        // the point of the arrow
        for ($j = $i; $j < $n; $j++)
        {
            echo " ";
        }
         
        // for printing the star(*)
        for ($j = $i; $j <= $n; $j++)
        {
            echo "*";
        }
        echo "\n";
    }
     
    // Prints lower part of the arrow
    for ($i = 2; $i <= $n; $i++)
    {
        // for the spacing to form
        // the point of the arrow
        for ($j = 1; $j < $i; $j++)
        {
            echo " ";
        }
         
        // for printing the star(*)
        for ($j = 1; $j <= $i; $j++)
        {
            echo "*";
        }
        echo "\n";
    }
}
 
// Driver code
$n = 5;
arrow($n);
 
// This code is contributed by mits
?>

Javascript

<script>
 
      // JavaScript Implementation
      // to print the pattern
 
      // arrow function
      function arrow(n)
      {
        // Prints the upper part of the arrow
        for (var i = 1; i <= n; i++)
        {
          // for the spacing to form
          // the point of the arrow
          for (var j = i; j < n; j++)
          {
            document.write("  ");
          }
 
          // for printing the star(*)
          for (var j = i; j <= n; j++)
          {
            document.write("*");
          }
          document.write("<br>");
        }
 
        // Prints lower part of the arrow
        for (var i = 2; i <= n; i++)
        {
          // for the spacing to form
          // the point of the arrow
          for (var j = 1; j < i; j++) {
            document.write("  ");
          }
 
          // for printing the star(*)
          for (var j = 1; j <= i; j++)
          {
            document.write("*");
          }
          document.write("<br>");
        }
      }
 
      // driver code
       
      // get the value from user
      var n = 5;
       
      // function calling
      arrow(n);
</script>

Producción :  

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

Complejidad de tiempo: O (n 2 ), donde n es el número de entrada dado.

Complejidad espacial: O (1), ya que no estamos utilizando ningún espacio adicional.
 

Publicación traducida automáticamente

Artículo escrito por MrinalVerma 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 *