Programas para imprimir Patrones Interesantes

Programa para imprimir el siguiente patrón: 

Ejemplos: 

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

Este programa se divide en cuatro partes.

C++

// C++ program to print
// the given pattern
#include<iostream>
using namespace std;
 
void pattern(int n)
{
    int i, j;
     
    // This is upper half of pattern
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= (2 * n); j++)
        {
             
            // Left part of pattern
            if (i > (n - j + 1))
                cout << " ";
            else
                cout << "*";
                  
            // Right part of pattern
            if ((i + n) > j)
                cout << " ";
            else
                cout << "*";
        }
        cout << endl ;
    }
      
    // This is lower half of pattern
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= (2 * n); j++)
        {
             
            // Right Part of pattern
            if (i < j)
                cout << " ";
            else
                cout << "*";
              
            // Left Part of pattern
            if (i <= ((2 * n) - j))
                cout << " ";
            else
                cout << "*";
        }
        cout << endl;
    }
}
  
// Driver Code
int main()
{
    pattern(7);
     
    return 0;
}
 
// This code is contributed by bunnyram19

C

// C program to print
// the given pattern
 
#include<stdio.h>
void pattern(int n)
{
    int i,j;
 
    // This is upper half of pattern
    for (i=1; i<=n; i++)
    {
        for (j=1; j<=(2*n); j++)
        {
            // Left part of pattern
            if (i>(n-j+1))
                printf(" ");
            else
                printf("*");
                 
            // Right part of pattern
            if ((i+n)>j)
                printf(" ");
            else
                printf("*");
        }
        printf("\n");
    }
     
    // This is lower half of pattern
    for (i=1; i<=n; i++)
    {
        for (j=1; j<=(2*n); j++)
        {
            // Right Part of pattern
            if (i<j)
                printf(" ");
            else
                printf("*");
             
            // Left Part of pattern
            if (i<=((2*n)-j))
                printf(" ");
            else
                printf("*");
        }
        printf("\n");
    }
}
 
// Driver Code
int main()
{
    pattern(7);
    return 0;
}

Java

// Java program to print
// the given pattern
import java.io.*;
 
class GFG {
     
    static void pattern(int n)
    {
        int i, j;
 
        // This is upper half of pattern
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= (2 * n); j++) {
                 
                // Left part of pattern
                if (i > (n - j + 1))
                    System.out.print(" ");
                else
                    System.out.print("*");
 
                // Right part of pattern
                if ((i + n) > j)
                    System.out.print(" ");
                else
                    System.out.print("*");
            }
             
            System.out.println("");
        }
 
        // This is lower half of pattern
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= (2 * n); j++) {
                 
                // Right Part of pattern
                if (i < j)
                    System.out.print(" ");
                else
                    System.out.print("*");
 
                // Left Part of pattern
                if (i <= ((2 * n) - j))
                    System.out.print(" ");
                else
                    System.out.print("*");
            }
             
            System.out.println("");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        pattern(7);
    }
}
 
// This code is contributed by vt_m

Python3

# Python3 program to print
# the given pattern
 
def pattern(n):
     
    # This is upper half of pattern
    for i in range (1, n + 1):
        for j in range (1, 2 * n):
             
            # Left part of pattern
            if i > (n - j + 1):
                print("", end = ' ');
            else:
                print("*", end = '');
                 
            # Right part of pattern
            if i + n - 1 > j:
                print("", end = ' ');
            else:
                print("*", end = '');
        print("");
         
    # This is lower half of pattern
    for i in range (1, n + 1):
        for j in range (1, 2 * n):
            #Left part of pattern
            if i < j:
                print("", end = ' ');
            else:
                print("*", end = '');
                 
            # Right part of pattern
            if i < 2 * n - j:
                print("", end = ' ');
            else:
                print("*", end = '');
        print("");
         
# Driver Code
pattern(7);
 
# This code is contributed by mits

C#

// C# program to print
// the given pattern
using System;
 
class GFG
{
    static void pattern(int n)
    {
        int i, j;
 
        // This is upper
        // half of pattern
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= (2 * n); j++)
            {
                 
                // Left part of pattern
                if (i > (n - j + 1))
                    Console.Write(" ");
                else
                    Console.Write("*");
 
                // Right part of pattern
                if ((i + n) > j)
                    Console.Write(" ");
                else
                    Console.Write("*");
            }
             
            Console.WriteLine("");
        }
 
        // This is lower
        // half of pattern
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= (2 * n); j++)
            {
                 
                // Right Part of pattern
                if (i < j)
                    Console.Write(" ");
                else
                    Console.Write("*");
 
                // Left Part of pattern
                if (i <= ((2 * n) - j))
                    Console.Write(" ");
                else
                    Console.Write("*");
            }
             
            Console.WriteLine("");
        }
    }
     
    // Driver Code
    static public void Main ()
    {
        pattern(7);
    }
}
 
// This code is contributed by ajit

PHP

<?php
// PHP program to print
// the given pattern
 
function pattern($n)
{
    $i; $j;
 
    // This is upper half of pattern
    for ($i = 1; $i <= $n; $i++)
    {
        for ($j = 1; $j <= (2 * $n); $j++)
        {
            // Left part of pattern
            if ($i > ($n - $j + 1))
                echo " ";
            else
                echo "*";
                 
            // Right part of pattern
            if (($i + $n) > $j)
                echo " ";
            else
                echo "*";
        }
        printf("\n");
    }
     
    // This is lower half of pattern
    for ($i = 1; $i <= $n; $i++)
    {
        for ($j = 1; $j <= (2 * $n); $j++)
        {
            // Right Part of pattern
            if ($i < $j)
                echo " ";
            else
                echo "*";
             
            // Left Part of pattern
            if ($i <= ((2 * $n) - $j))
                echo " ";
            else
                echo "*";
        }
        echo "\n";
    }
}
 
// Driver Code
pattern(7);
 
// This code is contributed by m_kit
?>

Javascript

<script>
 
      // JavaScript program to print
      // the given pattern
 
      function pattern(n) {
        var i, j;
 
        // This is upper half of pattern
        for (i = 1; i <= n; i++) {
          for (j = 1; j <= 2 * n; j++) {
            // Left part of pattern
            if (i > n - j + 1)
            document.write("  ");
            else
            document.write("*");
 
            // Right part of pattern
            if (i + n > j)
            document.write("  ");
            else
            document.write("*");
          }
          document.write("<br>");
        }
 
        // This is lower half of pattern
        for (i = 1; i <= n; i++) {
          for (j = 1; j <= 2 * n; j++) {
            // Right Part of pattern
            if (i < j)
            document.write("  ");
            else
            document.write("*");
 
            // Left Part of pattern
            if (i <= 2 * n - j)
            document.write("  ");
            else
            document.write("*");
          }
          document.write("<br>");
        }
      }
 
      // Driver Code
      pattern(7);
       
    </script>

Producción : 

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

Complejidad temporal: O(n 2 )

Espacio Auxiliar: O(1)

Programa para imprimir el siguiente patrón: 

Ejemplos: 

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

Este programa se divide en cuatro partes. 

C++

// C++ program to print the
// given pattern
#include <bits/stdc++.h>
using namespace std;
 
void pattern(int n)
{
    int i, j;
     
    // This is upper half of pattern
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= (2 * n); j++)
        {
            // Left part of pattern
            if (i < j)
                cout << " ";
            else
                cout << "*";
             
            // Right part of pattern
            if (i <= ((2 * n) - j))
                cout << " ";
            else
                cout << "*";
        }
        cout << "\n";
    }
     
    // This is lower half of pattern
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= (2 * n); j++)
        {
            // Left part of pattern
            if (i > (n - j + 1))
                cout <<" ";
            else
                cout <<"*";
                 
            // Right part of pattern
            if ((i + n) > j)
                cout << " ";
            else
                cout << "*";
        }
        cout << "\n";
    }
}
 
// Driver Code
int main()
{
    pattern(7);
    return 0;
}
 
// This code is contributed by shivanisinghss2110

C

// C program to print the
// given pattern
 
#include<stdio.h>
void pattern(int n)
{
    int i,j;
     
    // This is upper half of pattern
    for (i=1; i<=n; i++)
    {
        for (j=1; j<=(2*n); j++)
        {
            // Left part of pattern
            if (i<j)
                printf(" ");
            else
                printf("*");
             
            // Right part of pattern
            if (i<=((2*n)-j))
                printf(" ");
            else
                printf("*");
        }
        printf("\n");
    }
     
    // This is lower half of pattern
    for (i=1; i<=n; i++)
    {
        for (j=1;j<=(2*n);j++)
        {
            // Left part of pattern
            if (i>(n-j+1))
                printf(" ");
            else
                printf("*");
                 
            // Right part of pattern
            if ((i+n)>j)
                printf(" ");
            else
                printf("*");
        }
        printf("\n");
    }
}
 
// Driver Code
int main()
{
    pattern(7);
    return 0;
}

Java

// Java program to print the
// given pattern
 
import java.io.*;
 
class GFG {
 
    static void pattern(int n)
    {
        int i, j;
 
        // This is upper half of pattern
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= (2 * n); j++) {
                 
                // Left part of pattern
                if (i < j)
                    System.out.print(" ");
                else
                    System.out.print("*");
 
                // Right part of pattern
                if (i <= ((2 * n) - j))
                    System.out.print(" ");
                else
                    System.out.print("*");
            }
             
            System.out.println("");
        }
 
        // This is lower half of pattern
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= (2 * n); j++) {
                 
                // Left part of pattern
                if (i > (n - j + 1))
                    System.out.print(" ");
                else
                    System.out.print("*");
 
                // Right part of pattern
                if ((i + n) > j)
                    System.out.print(" ");
                else
                    System.out.print("*");
            }
             
            System.out.println("");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        pattern(7);
    }
}
 
// This code is contributed by vt_m

Python3

# Python3 program to
# print the given pattern
 
def pattern(n):
     
    # This is upper
    # half of pattern
    for i in range(1, n + 1):
        for j in range(1, 2 * n + 1):
             
            # Left part of pattern
            if (i < j):
                print("", end = " ");
            else:
                print("*", end = "");
                 
            # Right part of pattern
            if (i <= ((2 * n) - j)):
                print("", end = " ");
            else:
                print("*", end = "");
        print("");
     
    # This is lower
    # half of pattern
    for i in range(1, n + 1):
        for j in range(1, 2 * n + 1):
             
            # Left part of pattern
            if (i > (n - j + 1)):
                print("", end = " ");
            else:
                print("*", end = "");
             
            # Right part of pattern
            if ((i + n) > j):
                print("", end = " ");
            else:
                print("*", end = "");
        print("");
 
# Driver Code
pattern(7);
 
# This code is contributed
# by mits

C#

// C# program to print
// the given pattern
using System;
 
class GFG
{
    static void pattern(int n)
    {
        int i, j;
 
        // This is upper
        // half of pattern
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= (2 * n); j++)
            {
                 
                // Left part of pattern
                if (i < j)
                    Console.Write(" ");
                else
                    Console.Write("*");
 
                // Right part of pattern
                if (i <= ((2 * n) - j))
                    Console.Write(" ");
                else
                    Console.Write("*");
            }
             
            Console.WriteLine("");
        }
 
        // This is lower
        // half of pattern
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= (2 * n); j++)
            {
                 
                // Left part of pattern
                if (i > (n - j + 1))
                    Console.Write(" ");
                else
                    Console.Write("*");
 
                // Right part of pattern
                if ((i + n) > j)
                    Console.Write(" ");
                else
                    Console.Write("*");
            }
             
            Console.WriteLine("");
        }
    }
 
    // Driver Code
    static public void Main ()
    {
        pattern(7);
    }
}
 
// This code is contributed by ajit

PHP

<?php
// PHP program to print
// the given pattern
function pattern($n)
{
    $i; $j;
     
    // This is upper half
    // of pattern
    for ($i = 1; $i <= $n; $i++)
    {
        for ($j = 1; $j <= (2 * $n); $j++)
        {
            // Left part of pattern
            if ($i < $j)
                echo " ";
            else
                echo "*";
             
            // Right part of pattern
            if ($i <= ((2 * $n) - $j))
                echo " ";
            else
                echo "*";
        }
        echo "\n";
    }
     
    // This is lower half of pattern
    for ($i = 1; $i <= $n; $i++)
    {
        for ($j = 1;$j <= (2 * $n); $j++)
        {
            // Left part of pattern
            if ($i > ($n - $j + 1))
                echo " ";
            else
                echo "*";
                 
            // Right part of pattern
            if (($i + $n) > $j)
                echo " ";
            else
                echo "*";
        }
    echo "\n";
    }
}
 
// Driver Code
pattern(7);
 
// This code is contributed by aj_36
?>

Javascript

<script>
 
// Javascript program to print the
// given pattern  
function pattern(n)
{
    var i, j;
     
    // This is upper half of pattern
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= (2 * n); j++)
        {
             
            // Left part of pattern
            if (i < j)
                document.write("  ");
            else
                document.write("*");
     
            // Right part of pattern
            if (i <= ((2 * n) - j))
                document.write("  ");
            else
                document.write("*");
        }
        document.write('<br>');
    }
     
    // This is lower half of pattern
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= (2 * n); j++)
        {
             
            // Left part of pattern
            if (i > (n - j + 1))
                document.write("  ");
            else
                document.write("*");
     
            // Right part of pattern
            if ((i + n) > j)
                document.write("  ");
            else
                document.write("*");
        }
        document.write('<br>');
    }
}
 
// Driver Code
pattern(7);
 
// This code is contributed by Princi Singh
 
</script>

Producción : 

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

Complejidad temporal: O(n 2 )

Espacio Auxiliar: O(1)

Este artículo es una contribución de Aditya Kumar . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

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