Programa para imprimir patrones de flecha derecha e izquierda

Escriba en el programa para imprimir el patrón de flecha derecha y el patrón de flecha izquierda formado por estrellas. La entrada es un número impar n que representa la altura y el ancho del patrón que se imprimirá.
Ejemplos: 
 

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


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


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


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

C++

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

Java

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

Python3

# Python3 program to print Pyramid pattern
 
# function to print right arrow pattern
def rightpattern(n):
     
    # for printing upper portion
    c1 = (n - 1) // 2;
 
    # for printing lower portion
    c2 = 3 * n // 2 - 1;
 
    for i in range(n):
 
        for j in range(n):
 
            # checking conditions to print
            # right arrow
            if (j - i == c1 or
                i + j == c2 or i == c1):
 
                print("*", end = "");
                 
            # otherwise print space
            else:
 
                print(" ", end = "");
 
        # for jumping to next line
        print();
 
def string(n):
 
    for i in range(n):
        print(" ", end = "");
 
# function to print left arrow pattern
def leftpattern(n):
 
    s = ' ';
    st = '*';
 
    # for printing upper part
    for i in range((n - 1) // 2, 0, -1):
        string(i);
        print(st);
 
    # for printing middle part
    for i in range(n):
        print("*", end = "");
 
    print();
 
    # for printing lower part
    for i in range(1, ((n - 1) // 2) + 1):
        string(i);
        print(st);
 
    print();
 
# Driver Code
if __name__ == '__main__':
    n = 9; # Must be odd
 
    # function calling to print
    # right arrow
    rightpattern(n);
 
    print("");
 
    # function calling to print
    # left arrow
    leftpattern(n);
 
# This code is contributed by PrinciRaj1992

C#

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

PHP

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

Javascript

<script>
 
      // function to print right arrow pattern
      function rightpattern(n) {
        // for printing upper portion
        var c1 = (n - 1) / 2;
 
        // for printing lower portion
        var c2 = Math.floor((3 * n) / 2 - 1);
 
        for (var i = 0; i < n; i++) {
          for (var j = 0; j < n; j++) {
            // checking conditions to
            // print right arrow
            if (j - i === c1 || i + j === c2
            || i === c1)
            document.write("*");
            // otherwise print space
            else
            document.write("  ");
          }
 
          // for jumping to next line
          document.write("<br>");
        }
      }
 
      // function to print left
      // arrow pattern
      function leftpattern(n) {
        var s = " ";
        var st = "*";
 
        // for printing upper part
        for (var i = (n - 1) / 2; i > 0; i--) {
          for (var j = 0; j < i; j++)
          document.write("  ");
          document.write(st + "<br>");
        }
 
        // for printing middle part
        for (var i = 0; i < n; i++)
        document.write("*");
        document.write("<br>");
 
        // for printing lower part
        for (var i = 1; i <= (n - 1) / 2; i++)
        {
          for (var j = 0; j < i; j++)
          document.write("  ");
          document.write(st + "<br>");
        }
        document.write("<br>");
      }
 
      // Driver Code
      var n = 9; // Must be odd
      // function calling to
      // print right arrow
      rightpattern(n);
      document.write("<br><br>");
      // function calling to
      // print left arrow
      leftpattern(n);
       
</script>

Producción:  

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


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

Complejidad del tiempo: O(n^2)
 

Publicación traducida automáticamente

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