Programa para imprimir patrón de números

Tenemos que imprimir un patrón en el que la columna del medio contenga solo 1, las columnas del lado derecho contengan un dígito constante mayor que 1 y las columnas del lado izquierdo contengan un dígito constante mayor que 1. Cada fila debe verse como un palíndromo .

Ejemplos: 

Input : 3
Output :
    1
 2  1  2
    1

Input : 5
Output :
     1
  2  1  2
3 2  1  2 3
  2  1  2
     1

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

C++

// CPP program to print pattern
#include <bits/stdc++.h>
using namespace std;
 
void display()
{
     
    int n = 5;
    int space = n / 2, num = 1;
      
    // Outer for loop for
    // number of rows
    for (int i = 1; i <= n; i++)
    {
        // Inner for loop for
        // printing space
        for (int j = 1; j <= space; j++)           
            cout<<" ";
          
        // Logic for printing
        // the pattern for everyline
        int count = num / 2 + 1;
         
        for (int k = 1; k <= num; k++)
        {
            cout<<count;
             
            // Value of count decrements
            // in every cycle
            if (k <= num /2 )
                count--;
 
            // Value of count will
            // increment in every cycle
            else
                count++;
        }
 
        cout<<"\n";
 
        // Before reaching half Space
        // is decreased by 1 and num
        // is increased by 2
        if (i <= n / 2)
        {
            space = space - 1;
            num = num + 2;
        }
 
        // After reaching to half
        // space is increased by 1
        // and num is decreased by 2
        else
        {
            space = space + 1;
            num = num - 2;
        }
    }
}
 
// Driver Code
int main()
{
    display();
    return 0;
}
 
// This code is contributed by Nikita tiwari.

Java

// Java program to print above pattern
import java.util.Scanner;
class Pattern
{
    void display()
    {
        int n = 5;
        int space = n / 2, num = 1;
         
        // Outer for loop for
        // number of rows
        for (int i = 1; i <= n; i++)
        {
            // Inner for loop
            // for printing space
            for (int j = 1; j <= space; j++)           
                System.out.print(" ");
             
            // Logic for printing
            // the pattern for everyline
            int count = num / 2 + 1;
            for (int k = 1; k <= num; k++)
            {
                System.out.print(count);
                // Value of count decrements
                // in every cycle
                if (k <= num /2 )
                    count--;
 
                // Value of count will increment
                // in every cycle
                else
                    count++;
            }
 
            System.out.println();
 
            // Before reaching half Space is decreased
            // by 1 and num is increased by 2
            if (i <= n / 2)
            {
                space = space - 1;
                num = num + 2;
            }
 
            // After reaching to half space is increased
            // by 1 and num is decreased by 2
            else
            {
                space = space + 1;
                num = num - 2;
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        Pattern p = new Pattern();
        p.display();
    }
}

Python3

# Python 3 program to
# print above pattern
 
def display() :
    n = 5
    space = n // 2
    num = 1
     
    # Outer for loop for
    # number of rows
    for i in range(1, n+1) :
         
        # Inner for loop for 
        # printing space
        for j in range(1, space+1) :
            print(" ", end = "")
             
        # Logic for printing the
        # pattern for everyline
        count = num // 2 + 1
         
        for k in range(1, num+1) :
            print(count, end = "")
             
            # Value of count decrements
            # in every cycle
            if (k <= num // 2 ) :
                count = count -1
 
            # Value of count will
            # increment in every cycle
            else :
                count = count + 1
        print()
 
        # Before reaching half Space
        # is decreased by 1 and num
        # is increased by 2
        if (i <= n // 2) :
            space = space - 1
            num = num + 2
         
        # After reaching to half
        # space is increased by 1
        # and num is decreased by 2
        else :
            space = space + 1
            num = num - 2
             
# Driver Code
display()
 
#This code is contributed by Nikita Tiwari.

C#

// C# program to print above pattern
using System;
class Pattern
{
    void display()
    {
        int n = 5;
        int space = n / 2, num = 1;
         
        // Outer for loop for
        // number of rows
        for (int i = 1; i <= n; i++)
        {
            // Inner for loop
            // for printing space
            for (int j = 1; j <= space; j++)       
                Console.Write(" ");
             
            // Logic for printing
            // the pattern for everyline
            int count = num / 2 + 1;
            for (int k = 1; k <= num; k++)
            {
                Console.Write(count);
                // Value of count decrements
                // in every cycle
                if (k <= num /2 )
                    count--;
 
                // Value of count will increment
                // in every cycle
                else
                    count++;
            }
 
            Console.WriteLine();
 
            // Before reaching half Space is decreased
            // by 1 and num is increased by 2
            if (i <= n / 2)
            {
                space = space - 1;
                num = num + 2;
            }
 
            // After reaching to half space is increased
            // by 1 and num is decreased by 2
            else
            {
                space = space + 1;
                num = num - 2;
            }
        }
    }
 
    // Driver Code
    public static void Main()
    {
        Pattern p = new Pattern();
        p.display();
    }
}
// This code is contributed by vt_m.

PHP

<?php
// php program to print
// above pattern
 
    function display($n)
    {
        $space = $n / 2;
        $num = 1;
         
        // Outer for loop for
        // number of rows
        for ($i = 1; $i <= $n; $i++)
        {
            // Inner for loop
            // for printing space
            for ($j = 1; $j <= $space; $j++)    
                echo " ";
             
            // Logic for printing
            // the pattern for everyline
            $count = $num / 2 + 1;
            for ($k = 1; $k <= $num; $k++)
            {
                echo floor($count);
                // Value of count decrements
                // in every cycle
                if ($k <= $num /2 )
                    $count--;
 
                // Value of count will increment
                // in every cycle
                else
                    $count++;
            }
 
            echo "\n";
 
            // Before reaching half Space
            // is decreased by 1 and
            // num is increased by 2
            if ($i <= $n / 2)
            {
                $space = $space - 1;
                $num = $num + 2;
            }
 
            // After reaching to half
            // space is increased by 1
            // and num is decreased by 2
            else
            {
                $space = $space + 1;
                $num = $num - 2;
            }
        }
    }
 
// Driver Code
$n = 5;
display($n);
 
// This code is contributed by mits
?>

Javascript

<script>
 
 // JavaScript program to print pattern
 
      function display() {
        var n = 5;
        var space = parseInt(n / 2),
          num = 1;
 
        // Outer for loop for
        // number of rows
         
        for (var i = 1; i <= n; i++) {
          // Inner for loop for
          // printing space
          for (var j = 1; j <= space; j++)
          document.write("  ");
 
          // Logic for printing
          // the pattern for everyline
          var count = parseInt(num / 2 + 1);
 
          for (var k = 1; k <= num; k++) {
            document.write(count);
 
            // Value of count decrements
            // in every cycle
            if (k <= num / 2) count--;
             
            // Value of count will
            // increment in every cycle
            else count++;
          }
 
          document.write("<br>");
 
          // Before reaching half Space
          // is decreased by 1 and num
          // is increased by 2
          if (i <= n / 2) {
            space = space - 1;
            num = num + 2;
          }
 
          // After reaching to half
          // space is increased by 1
          // and num is decreased by 2
          else {
            space = space + 1;
            num = num - 2;
          }
        }
      }
 
      // Driver Code
       
      display();
       
</script>

Producción : 

  1
 212
32123
 212
  1

Complejidad de tiempo: O(n 2 ), donde n representa la entrada dada.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Publicación traducida automáticamente

Artículo escrito por Bishal Kumar Dubey 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 *