Programa para Imprimir el Patrón de Trapecio

Dado ‘num’ que indica el número de líneas. La tarea es imprimir un patrón de trapecio en números de líneas.
Ejemplos: 
 

Input  : 4 
Output :
1*2*3*4*17*18*19*20
  5*6*7*14*15*16
    8*9*12*13
      10*11


Input : 2
Output :
1*2*5*6
  3*4

Algoritmo: 
paso 1. Para leer num que indica el número de líneas. 
paso 2. Estamos dividiendo el patrón en 2 mitades que son la parte LHS y la parte RHS. 
Ej: Cuando num = 2 
LHS – 
1*2* 
3*
RHS – 
5*6 

paso 3. Combinando LHS y RHS obtenemos el patrón completo.
 

C++

// CPP program to print Trapezium Pattern
#include <iostream>
 
using namespace std;
 
int main()
{
 
    int num = 3;
    int space;
 
    int i, j, lterm, rterm;
 
    // The terms on the LHS of the pattern
    lterm = 1;
 
    // The terms on the RHS of the pattern
    rterm = num * num + 1;
 
    for (i = num; i > 0; i--) {
 
        // To print number of spaces
        for (space = num; space > i; space--)
            cout << " ";
 
        for (j = 1; j <= i; j++) {
            cout << lterm;
            cout << "*";
            lterm++;
        }
        for (j = 1; j <= i; j++) {
            cout << rterm;
            if (j < i)
                printf("*");
            rterm++;
        }
 
        // To get the next term on RHS of the Pattern
        rterm = rterm - (i - 1) * 2 - 1;
        cout << endl;
    }
}

Java

// Java program to print Trapezium Pattern
public class HelloWorld {
 
    public static void trapeziumPattern(int num)
    {
 
        int firsthalf = 1;
        int secondhalf = (num * num) + 1;
        int numOfSpaces = 0;
 
        // numOfLines is the line number
        for (int numOfLines = num; numOfLines >= 1;
             numOfLines--) {
 
            // Prints the spaces for each line
            for (int numOfSpacesCounter = numOfSpaces;
                 numOfSpacesCounter >= 1;
                 numOfSpacesCounter--) {
                System.out.print(" ");
            }
 
            // Prints the first half of the trapezium
            for (int firstHalfCounter = 1;
                 firstHalfCounter <= numOfLines;
                 firstHalfCounter++) {
 
                // If it is the last number for a line then
                // we don't print '*'
                if (firstHalfCounter == numOfLines)
                    System.out.print((firsthalf++));
 
                else
                    System.out.print((firsthalf++) + "*");
            }
 
            // Prints the second half of the trapezium
            for (int secondHalfCounter = 1;
                 secondHalfCounter <= numOfLines;
                 secondHalfCounter++) {
                System.out.print("*" + (secondhalf++));
            }
 
            System.out.println();
 
            // Calculates the number of Spaces for the next
            // line
            numOfSpaces += 2;
 
            // Calculates the first number of the
            // second half for the next iteration/line
            secondhalf
                = (secondhalf - 1) - ((numOfLines - 1) * 2);
        }
    }
 
    public static void main(String[] args)
    {
        trapeziumPattern(
            4); // Passing the integer as the argument to
                // print trapezium pattern
    }
}

Python 3

# Python 3 program to print
# Trapezium Pattern
  
if __name__ == "__main__":
    num = 3
  
    # The terms on the LHS
    # of the pattern
    lterm = 1
  
    # The terms on the RHS
    # of the pattern
    rterm = num * num + 1
  
    for i in range(num, -1, -1):
  
        # To print number of spaces
        for space in range(num, i-1, -1):
            print(" ", end ="")
  
        for j in range(1, i + 1):
            print(str(lterm)+"*", end ="")
            lterm += 1
 
        for j in range(1, i + 1):
            print(rterm, end ="")
            if j < i:
                print("*", end ="")
            rterm += 1
  
        # To get the next term on RHS of the Pattern
        rterm = rterm - (i - 1) * 2 - 1
        print()
 
# This code is contributed by ChitraNayal

C#

// C# program to print Trapezium Pattern
using System;
 
public class HelloWorld {
 
    public static void Main(String[] args)
    {
 
        // Scanner scn = new Scanner(System.in);
        int num = 3;
        int space;
        // System.out.println("Enter number of lines : ");
        // num = scn.nextInt();
 
        int i, j, lterm, rterm;
 
        lterm = 1; // The terms on the LHS of the pattern
 
        // The terms on the RHS of the pattern
        rterm = num * num + 1;
 
        for (i = num; i > 0; i--) {
 
            // To print number of spaces
            for (space = num; space > i; space--)
                Console.Write(" ");
 
            for (j = 1; j <= i; j++) {
                Console.Write(lterm);
                Console.Write("*");
                lterm++;
            }
            for (j = 1; j <= i; j++) {
                Console.Write(rterm);
                if (j < i)
                    Console.Write("*");
                rterm++;
            }
 
            // To get the next term on RHS of the Pattern
            rterm = rterm - (i - 1) * 2 - 1;
            Console.WriteLine();
        }
    }
}
 
// This code is contributed by ankita_saini

PHP

<?php
// PHP program to print
// Trapezium Pattern
$num = 3;
$space;
 
$i; $j; $lterm; $rterm;
 
// The terms on the LHS
// of the pattern
$lterm = 1;
 
// The terms on the
// RHS of the pattern
$rterm = $num * $num + 1;
 
for ($i = $num; $i > 0; $i--)
{
 
    // To print number of spaces
    for ($space = $num;
         $space > $i; $space--)
        echo " ";
 
    for ($j = 1; $j <= $i; $j++)
    {
        echo $lterm;
        echo "*";
        $lterm++;
    }
    for ($j = 1; $j <= $i; $j++)
    {
        echo $rterm;
        if ($j < $i)
            echo "*";
        $rterm++;
    }
 
    // To get the next term
    // on RHS of the Pattern
    $rterm = $rterm - ($i - 1) * 2 - 1;
    echo "\n";
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>

Javascript

<script>
 
      // JavaScript program to print Trapezium Pattern
      var num = 3;
      var space;
 
      var i, j, lterm, rterm;
 
      // The terms on the LHS of the pattern
      lterm = 1;
 
      // The terms on the RHS of the pattern
      rterm = num * num + 1;
 
      for (i = num; i > 0; i--) {
        // To print number of spaces
        for (space = num; space > i; space--)
        document.write("  ");
 
        for (j = 1; j <= i; j++) {
          document.write(lterm);
          document.write("*");
          lterm++;
        }
        for (j = 1; j <= i; j++) {
          document.write(rterm);
          if (j < i) document.write("*");
          rterm++;
        }
 
        // To get the next term on RHS of the Pattern
        rterm = rterm - (i - 1) * 2 - 1;
        document.write("<br>");
      }
       
</script>

Producción: 

Enter number of lines : 3
1*2*3*10*11*12
  4*5*8*9
    6*7

Complejidad temporal: O(n 2 )

complejidad del espacio: O(1)
 

Publicación traducida automáticamente

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