Programa para imprimir pirámide hueca, patrón de diamantes y sus modificaciones.

Requisito previo: bucles , instrucción If Else
1. Patrón de pirámide/triángulo hueco 
El patrón es similar al patrón de pirámide. La única diferencia es que reemplazaremos todos los caracteres internos ‘#’ o ‘*’ por caracteres de espacio e imprimiremos 2*N-1 (N = número de filas en el patrón) caracteres ‘#’ o ‘*’ en la última fila . 
Ejemplos: 
 

Input: n=6
Output:
     #
    # #
   #   #
  #     #
 #       #
#         #
###########    

C++14

// CPP program to print a hollow pyramid pattern
#include <iostream>
using namespace std;
void printPattern(int);
int main()
{
    int n = 6;
 
    printPattern(n);
}
void printPattern(int n)
{
    int i, j, k = 0;
    for (i = 1; i <= n; i++) // row=6
    {
        // Print spaces
        for (j = i; j < n; j++) {
            cout << " ";
        }
        // Print #
        while (k != (2 * i - 1)) {
            if (k == 0 || k == 2 * i - 2)
                cout << "#";
            else
                cout << " ";
            k++;
        }
        k = 0;
        cout << endl; // print next row
    }
    // print last row
    for (i = 0; i < 2 * n - 1; i++) {
        cout << "#";
    }
}
// this article is contributed by Shivani Ghughtyal

Java

// JAVA program to print a hollow
// pyramid pattern
class GFG{
     
    public static void main(String args[])
    {
        int n = 6;
     
        printPattern(n);
    }
     
    static void printPattern(int n)
    {
        int i, j, k = 0;
        for (i = 1; i <= n; i++) // row=6
        {
            // Print spaces
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
            // Print #
            while (k != (2 * i - 1)) {
                if (k == 0 || k == 2 * i - 2)
                    System.out.print("#");
                else
                    System.out.print(" ");
                k++;
                ;
            }
            k = 0;
             
            // print next row
            System.out.println();
        }
        // print last row
        for (i = 0; i < 2 * n - 1; i++) {
            System.out.print("#");
        }
    }
}
 
/*This code is contributed by Nikita Tiwari.*/

Python

# Python program to print a hollow
# pyramid pattern
 
def printPattern( n) :
    k = 0
    for i in range(1,n+1) : #row 6
     
        # Print spaces
        for j in range(i,n) :
            print(' ', end='')
         
        # Print #
        while (k != (2 * i - 1)) :
            if (k == 0 or k == 2 * i - 2) :
                print('#', end='')
            else :
                print(' ', end ='')
            k = k + 1
        k = 0;
        print ("") # print next row
         
    # print last row
    for i in range(0, 2 * n -1) :
        print ('#', end = '')
 
# Driver code
n = 6
printPattern(n)
 
# This code is contributed by Nikita Tiwari.

PHP

<?php
// php program to print a
// hollow pyramid pattern
 
function printPattern($n)
{
    $k = 0;
     
    // row=6
    for ($i = 1; $i <= $n; $i++)
    {
         
        // Print spaces
        for ($j = $i; $j < $n; $j++)
        {
            echo " ";
        }
         
        // Print #
        while ($k != (2 * $i - 1))
        {
            if ($k == 0 || $k == 2 *
                             $i - 2)
                echo "#";
            else
                echo " ";
            $k++;
        }
        $k = 0;
         
        // print next row
        echo "\n";
    }
     
    // print last row
    for ($i = 0; $i < 2 * $n - 1; $i++)
    {
        echo "#";
    }
}
 
//Driver Code
$n = 6;
printPattern($n);
 
// This code is contributed by mits
?>

Javascript

<script>
      // JavaScript program to print a hollow pyramid pattern
      var n = 6;
      printPattern(n);
      function printPattern(n)
      {
        var i, j, k = 0;
        for (i = 1; i <= n; i++)// row=6
        {
          // Print spaces
          for (j = i; j < n; j++)
          {
            document.write("  ");
          }
           
          // Print #
          while (k != 2 * i - 1)
          {
            if (k == 0 || k == 2 * i - 2)
                document.write("#");
            else
                document.write("  ");
            k++;
          }
          k = 0;
          document.write("<br>"); // print next row
        }
         
        // print last row
        for (i = 0; i < 2 * n - 1; i++)
        {
          document.write("#");
        }
      }
       
      // This code is contributed by rdtank.
    </script>

C++14

#include <bits/stdc++.h>
using namespace std;
 
// function to print the pattern
void printPattern(int& n)
{
    int i,j,mid;
    if(n%2==1) //when n is odd, increase it by 1 to make it even
      n++;
    mid = n/2;
     
    // upper half pattern
    for(i = 1; i<= mid; i++) {
      for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box before star
         cout<<" ";
          
      if(i == 1) {
         cout << "*";
      }else{
         cout << "*"; //in each line star at start and end position
         for(j = 1; j<=2*i-3; j++) { //print space to make hollow
            cout << " ";
         }
         cout << "*";
      }
      for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star
         cout<<" ";
          
      cout << endl;
   }
    
   // lower half pattern
   for(i = mid+1; i<n; i++) {
        
      for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star
         cout<<" ";
          
      if(i == n-1) {
         cout << "*";
      }else{
         cout << "*"; //in each line star at start and end position
         for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
            cout << " ";
         }
         cout << "*";
      }
      for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star
         cout<<" ";
          
      cout << endl;
   }
}
 
// driver's code
int main() {
   int n=7;
   printPattern(n);
}
// this code is contributed by prophet1999

Java

// JAVA program
class GFG{
     
// function to print the pattern
static void printPattern(int n)
{
    int i,j,mid;
    if(n%2==1) //when n is odd, increase it by 1 to make it even
      n++;
    mid = n/2;
      
    // upper half pattern
    for(i = 1; i<= mid; i++) {
      for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box before star
         System.out.print(" ");
           
      if(i == 1) {
         System.out.print("*");
      }else{
         System.out.print("*"); //in each line star at start and end position
         for(j = 1; j<=2*i-3; j++) { //print space to make hollow
            System.out.print(" ");
         }
         System.out.print("*");
      }
      for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star
         System.out.print(" ");
           
      System.out.println();
   }
     
   // lower half pattern
   for(i = mid+1; i<n; i++) {
         
      for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star
         System.out.print(" ");
           
      if(i == n-1) {
         System.out.print("*");
      }else{
         System.out.print("*"); //in each line star at start and end position
         for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
            System.out.print(" ");
         }
         System.out.print("*");
      }
      for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star
         System.out.print(" ");
           
      System.out.println();
   }
         
}
     
    // driver's code
    public static void main(String args[])
    {
        int n = 7;
        printPattern(n);
    }
}
 
// This code is contributed by Pushpesh Raj.

C++14

#include <bits/stdc++.h>
using namespace std;
 
// function to print the pattern
void printPattern(int& n)
{
    int i,j,mid;
    if(n%2==1) //when n is odd, increase it by 1 to make it even
      n++;
    mid = n/2;
     
    // upper half pattern
    for(i = 1; i<= mid; i++) {
      for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box before star
         if(i==1)
         cout<<"-";
         else if(j==1)
         cout << "|";
         else cout<<" ";
      }
      if(i == 1) {
         cout << "*";
      }else{
         cout << "*"; //in each line star at start and end position
         for(j = 1; j<=2*i-3; j++) { //print space to make hollow
            cout << " ";
         }
         cout << "*";
      }
      for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box after star
         if(i==1)
         cout<<"-";
         else if(j==mid-i)
         cout << "|";
         else cout<<" ";
      }
      cout << endl;
   }
    
   // lower half pattern
   for(i = mid+1; i<n; i++) {
        
      for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box before star
         if(i==n-1)
         cout<<"-";
         else if(j==1)
         cout << "|";
         else cout<<" ";
      }
      if(i == n-1) {
         cout << "*";
      }else{
         cout << "*"; //in each line star at start and end position
         for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
            cout << " ";
         }
         cout << "*";
      }
      for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box after star
         if(i==n-1)
         cout<<"-";
         else if(j==i-mid)
         cout << "|";
         else cout<<" ";
      }
      cout << endl;
   }
}
 
// driver's code
int main() {
   int n=12;
   printPattern(n);
}
// this code is contributed by prophet1999

Java

// Java code to implement the above approach
 
import java.io.*;
import java.util.*;
  
class GFG
{
  // function to print the pattern
  public static void printPattern(int n)
  {
      int i,j,mid;
      if(n%2==1) //when n is odd, increase it by 1 to make it even
      n++;
      mid = n/2;
       
      // upper half pattern
      for(i = 1; i<= mid; i++) {
          for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box before star
          if(i==1)
          System.out.print("-");
          else if(j==1)
          System.out.print("|");
          else
          System.out.print(" ");
          }
           
      if(i == 1) {
         System.out.print("*");
      }
      else{
         System.out.print("*"); //in each line star at start and end position
         for(j = 1; j<=2*i-3; j++) { //print space to make hollow
            System.out.print(" ");
         }
         System.out.print("*");
      }
      for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box after star
         if(i==1)
         System.out.print("-");
         else if(j==mid-i)
         System.out.print("|");
         else System.out.print(" ");
      }
      System.out.print("\n");
   }
    
   // lower half pattern
   for(i = mid+1; i<n; i++) {
        
      for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box before star
         if(i==n-1)
         System.out.print("-");
         else if(j==1)
         System.out.print("|");
         else System.out.print(" ");
      }
      if(i == n-1) {
         System.out.print("*");
      }else{
         System.out.print("*"); //in each line star at start and end position
         for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
            System.out.print(" ");
         }
         System.out.print("*");
      }
      for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box after star
         if(i==n-1)
         System.out.print("-");
         else if(j==i-mid)
         System.out.print("|");
         else System.out.print(" ");
      }
      System.out.print("\n");
   }
  }
   
  // Driver Code
  public static void main(String[] args)
  {
    int n=12;
    printPattern(n);
  }
}
 
//this code is contributed by adityapatil12

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 *