Programa Java para imprimir el patrón de estrella en forma de diamante

En este artículo, vamos a aprender cómo imprimir patrones de estrellas en forma de diamante en Java. 

Ilustración:

Input: number = 7
 
Output:

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

Métodos: cuando se trata de la impresión de patrones, optamos por formas estándar de imprimirlos solo a través de bucles. Probaremos diferentes tipos de bucles para imprimir el mismo patrón.

Ejemplo 1: Uso del bucle do-while

Java

// Java program to Print Diamond Star Pattern
// Using do-while loop
 
// Importing input output classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variables
 
        // Variable initialized to the row where max star
        // should be there as after that they decreases to
        // give diamond pattern
        int number = 7;
 
        // Diamond starting with single star in first row,so
        // initialized
        int m = 1;
 
        // Columnar printing
        int n;
 
        // Outer loop 1
        // Prints the first half diamond
        do {
            n = 1;
 
            // Inner loop 1
            // Prints space until ++n <= number - m + 1 is
            // false
            do {
                // Print whitespace between
                System.out.print(" ");
 
            }
 
            // Condition for inside do-while loop 1
            while (++n <= number - m + 1);
 
            // Now
            n = 1;
 
            // Inner loop 2
            // Prints star until ++n <= m * 2 - 1 is false
 
            do {
 
                // Print star
                System.out.print("*");
            }
 
            // Condition for inner do-while loop 2
            while (++n <= m * 2 - 1);
 
            // A new row requires a new line
            System.out.println();
 
        }
 
        // Condition for outer do-while loop 1
        while (++m <= number);
 
        // Now we are done with printing the upper half
        // diamond.
 
        // Note: Not to print the bottom row again in lower
        // half diamond printing Hence variable to be
        // initialized should one lesser than number
        m = number - 1;
 
        // Outer loop 2
        // Prints the second half diamond
        do {
            n = 1;
 
            // Inner loop 1
            // Prints space until ++n <= number - m + 1 is
            // false
            do {
                // Print whitespace between
                System.out.print(" ");
 
            } while (++n <= number - m + 1);
 
            n = 1;
 
            // Inner loop 2
            // Prints star until ++n <= m * 2 - 1 is false
            do {
                // Prints star
                System.out.print("*");
 
            } while (++n <= m * 2 - 1);
 
            // By now done with one row of lower diamond
            // printing so a new line is required
            System.out.println();
 
        }
 
        // Condition for outer do-while loop 2
        while (--m > 0);
    }
}
Producción

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

Ejemplo 2: Uso del ciclo while

Java

// Java program to print diamond star pattern
// Using while loop
 
// Importing input output classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring and initializing variables
 
        // Variable initialized to the row where max star
        // should be there as after that they decreases to
        // give diamond pattern
        int number = 7;
 
        // Diamond starting with single star in first row
        int m = 1;
 
        // Columnar printing
        int n;
 
        // Outer loop 1
        // Prints the first half diamond
 
        // Condition holding true till
        // number of rows initialized
        while (m <= number) {
            n = 1;
 
            // Inner loop 1
            // Prints space until n++ <= number - m is false
            while (n++ <= number - m) {
 
                // Print whitespaces inbetween
                System.out.print(" ");
            }
 
            n = 1;
 
            // Inner loop 2
            // Prints star until n++ <= m * 2 - 1 is false
            while (n++ <= m * 2 - 1) {
 
                // Print star
                System.out.print("*");
            }
 
            // By now we are done for above pyramid printing
            // ending line after each row
            System.out.println();
 
            // Incrementing as we want pyramid generation
            m++;
        }
 
        // Now we are done with printing the upper half
        // diamond.
 
        // Note: Not to print the bottom row again in lower
        // half diamond printing Hence variable t be
        // initialized should one lesser than number
        m = number - 1;
 
        // Outer loop 2
        // Prints the second half diamond
        while (m > 0) {
            n = 1;
 
            // Inner loop 1
            // Prints spaces until n++ <= number - m is
            // false
            while (n++ <= number - m) {
 
                // Print whitespace in between
                System.out.print(" ");
            }
 
            n = 1;
 
            // Inner loop 2
            // Prints star until n++ <= m * 2 - 1 is false
            while (n++ <= m * 2 - 1) {
 
                // Print star
                System.out.print("*");
            }
 
            // Ending line after each row
            System.out.println();
 
            // Decrementing as we want reverse pyramid
            // generation
            m--;
        }
    }
}
Producción

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

Ejemplo 3: Uso del bucle for

Java

// Java program to print diamond star pattern
// Using for loop
 
// Importing  input output classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring and initializing variables
 
        // Variable initialized to the row where max star
        // should be there as after that they decreases to
        // give diamond pattern
        int number = 7;
 
        int m, n;
 
        // Outer loop 1
        // prints the first half diamond
        for (m = 1; m <= number; m++) {
 
            // Inner loop 1 print whitespaces inbetween
            for (n = 1; n <= number - m; n++) {
                System.out.print(" ");
            }
 
            // Inner loop 2 prints star
            for (n = 1; n <= m * 2 - 1; n++) {
                System.out.print("*");
            }
 
            // Ending line after each row
            System.out.println();
        }
 
        // Outer loop 2
        // Prints the second half diamond
        for (m = number - 1; m > 0; m--) {
 
            // Inner loop 1 print whitespaces inbetween
            for (n = 1; n <= number - m; n++) {
                System.out.print(" ");
            }
 
            // Inner loop 2 print star
            for (n = 1; n <= m * 2 - 1; n++) {
                System.out.print("*");
            }
 
            // Ending line after each row
            System.out.println();
        }
    }
}
Producción

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

Complejidad de Tiempo : O(n 2 )
Espacio Auxiliar : O(1)

Publicación traducida automáticamente

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