geometry - Print triangle (Half Pyramid) pattern using Java

Geometry - Print triangle (Half Pyramid) pattern using Java

To print a half-pyramid (right-angled triangle) pattern in Java, you can use nested loops. The outer loop will handle the rows, and the inner loop will handle the columns within each row.

Here's a simple Java program to print a half-pyramid pattern:

Code Example

public class HalfPyramid { public static void main(String[] args) { int numberOfRows = 5; // Number of rows for the half-pyramid // Outer loop for each row for (int i = 1; i <= numberOfRows; i++) { // Inner loop for printing spaces for (int j = 1; j <= i; j++) { System.out.print("* "); } // Move to the next line after each row System.out.println(); } } } 

Explanation

  1. numberOfRows: This variable defines the number of rows in the half-pyramid. You can adjust this to get a different height of the pyramid.

  2. Outer Loop (for (int i = 1; i <= numberOfRows; i++)): This loop iterates over the rows. For each iteration, it represents a new row in the pyramid.

  3. Inner Loop (for (int j = 1; j <= i; j++)): This loop prints the stars (*) for the current row. The number of stars printed in each row increases with each iteration of the outer loop.

  4. System.out.println();: After printing all stars in a row, this line moves the cursor to the next line.

Output

If numberOfRows is set to 5, the output will be:

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

Variations

  1. Printing with Numbers:

    To print numbers instead of stars:

    public class NumberPyramid { public static void main(String[] args) { int numberOfRows = 5; // Number of rows for the pyramid // Outer loop for each row for (int i = 1; i <= numberOfRows; i++) { // Inner loop for printing numbers for (int j = 1; j <= i; j++) { System.out.print(j + " "); } // Move to the next line after each row System.out.println(); } } } 

    Output:

    1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 
  2. Printing with Different Characters:

    To print with different characters:

    public class CharacterPyramid { public static void main(String[] args) { int numberOfRows = 5; // Number of rows for the pyramid char character = '#'; // Character to print // Outer loop for each row for (int i = 1; i <= numberOfRows; i++) { // Inner loop for printing characters for (int j = 1; j <= i; j++) { System.out.print(character + " "); } // Move to the next line after each row System.out.println(); } } } 

    Output:

    # # # # # # # # # # # # # # # 

You can modify the numberOfRows and characters to suit your needs. This pattern provides a foundation for understanding nested loops and formatting output in Java.

Examples

  1. How to print a half-pyramid triangle pattern in Java with numbers?

    • Description: This query provides a method to print a half-pyramid pattern using numbers.
    • Code:
      public class HalfPyramid { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } } } 
  2. How to create a half-pyramid pattern with stars in Java?

    • Description: This query demonstrates how to print a half-pyramid pattern using stars (*).
    • Code:
      public class HalfPyramidStars { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("* "); } System.out.println(); } } } 
  3. How to print a right-angled triangle pattern with numbers in Java?

    • Description: This query shows how to print a right-angled triangle using numbers in Java.
    • Code:
      public class RightAngledTriangle { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } } } 
  4. How to print a half-pyramid pattern with decreasing numbers in Java?

    • Description: This query covers printing a half-pyramid where numbers decrease in each row.
    • Code:
      public class DecreasingNumbersPyramid { public static void main(String[] args) { int rows = 5; for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } } } 
  5. How to print a half-pyramid pattern with alphabet characters in Java?

    • Description: This query demonstrates how to print a half-pyramid pattern using alphabet characters.
    • Code:
      public class HalfPyramidAlphabets { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { char ch = 'A'; for (int j = 1; j <= i; j++) { System.out.print(ch + " "); ch++; } System.out.println(); } } } 
  6. How to print a half-pyramid pattern with spaces for alignment in Java?

    • Description: This query shows how to print a half-pyramid with additional spaces for proper alignment.
    • Code:
      public class AlignedHalfPyramid { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = i; j < rows; j++) { System.out.print(" "); // Print spaces for alignment } for (int k = 1; k <= i; k++) { System.out.print("* "); } System.out.println(); } } } 
  7. How to print a half-pyramid pattern with a specific height in Java?

    • Description: This query covers printing a half-pyramid with a user-defined height.
    • Code:
      import java.util.Scanner; public class CustomHeightPyramid { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the height of the pyramid: "); int rows = scanner.nextInt(); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("* "); } System.out.println(); } scanner.close(); } } 
  8. How to print a half-pyramid pattern with a combination of numbers and stars in Java?

    • Description: This query shows how to print a half-pyramid pattern combining numbers and stars.
    • Code:
      public class NumberStarPyramid { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { if (j % 2 == 0) { System.out.print("* "); } else { System.out.print(j + " "); } } System.out.println(); } } } 
  9. How to print an inverted half-pyramid pattern in Java?

    • Description: This query demonstrates how to print an inverted half-pyramid pattern.
    • Code:
      public class InvertedHalfPyramid { public static void main(String[] args) { int rows = 5; for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print("* "); } System.out.println(); } } } 
  10. How to print a half-pyramid pattern with odd numbers in Java?

    • Description: This query covers printing a half-pyramid pattern using odd numbers.
    • Code:
      public class OddNumberPyramid { public static void main(String[] args) { int rows = 5; int number = 1; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(number + " "); number += 2; // Increment by 2 for odd numbers } System.out.println(); } } } 

More Tags

cryptography orchardcms base64 word-boundary node-postgres guice membership visualization windows-server-2012-r2 appium

More Programming Questions

More Transportation Calculators

More Various Measurements Units Calculators

More Mixtures and solutions Calculators

More Internet Calculators