Java Program to Print an Inverted Right Triangle Star Pattern

Introduction

Printing various patterns is a common exercise for beginners in programming, helping to understand loops and control structures. In this guide, we will create a Java program to print an inverted right triangle star pattern.

Problem Statement

Create a Java program that:

  • Accepts the size of the triangle (number of rows).
  • Prints an inverted right triangle using stars (*).

Example:

  • Input: size = 5
  • Output:
    *****
    ****
    ***
    **
    *
    

Solution Steps

  1. Input the Size of the Triangle: The size represents the number of rows in the inverted triangle.
  2. Use Nested Loops: Use nested loops to print stars (*) in decreasing order of length.
  3. Display the Pattern: Print each row with the correct number of stars, decreasing row by row.

Java Program

// Java Program to Print an Inverted Right Triangle Star Pattern
// Author: [Your Name]

import java.util.Scanner;

public class InvertedRightTriangle {
    public static void main(String[] args) {
        // Step 1: Accept the size of the triangle
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of the inverted right triangle: ");
        int size = sc.nextInt();

        // Step 2: Outer loop for the rows
        for (int i = size; i >= 1; i--) {
            // Step 3: Inner loop to print stars for each row
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            // Step 4: Move to the next line after each row
            System.out.println();
        }

        // Closing the scanner object
        sc.close();
    }
}

Explanation

Step 1: Input Size

  • The program starts by taking input from the user for the size of the triangle using the Scanner object.

Step 2: Outer Loop for Rows

  • The outer loop controls the number of rows, starting from the size down to 1. This loop dictates how many stars will be printed in each row.

Step 3: Inner Loop for Stars

  • The inner loop prints stars (*) for each row. The number of stars printed corresponds to the current row number, which decreases in each iteration.

Step 4: New Line After Each Row

  • After printing each row, the program moves to the next line to begin printing the next row of stars.

Output Example

For size = 5, the output is:

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

For size = 7, the output is:

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

Conclusion

This Java program prints an inverted right triangle using stars (*). The number of stars decreases row by row, creating the inverted shape. This exercise is a useful way to practice working with loops and pattern generation in Java.

Continue Learning with Java Guides

Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.

By Ramesh Fadatare - Udemy Instructor Partner

Comments