Java Program to Print Right Triangle Star Pattern

  • Write a Java program to print right angled triangle star pattern.

A Right triangle star pattern contains N space separated '*' characters in Nth row. In this program, we will use two for loops, outer for loop will print one row in every iteration whereas inner for loop will print N '*' characters for Nt row.

Sample Output
*
* *
* * *
* * * *
* * * * *
Algorithm to print right triangle star pattern
  • Take the number of rows of right triangle as input from user and store it in an integer variable N
  • Number of star characters in Kth row is always K. 1st row contains 1 star, 2nd row contains 2 stars. In general, Kth row contains K stars.
  • We will use two for loops to print right triangle star pattern.
    • Each iteration of outer loop will print one row of the pattern. For a right triangle star pattern of N rows, outer for loop will iterate N time.
    • Each iteration of inner loop will print one star (*). For Kth row of right triangle pattern, inner loop will iterate K times.

Java program to print triangle star pattern

package com.tcc.java.programs;

import java.util.*;

public class RightTrianglePattern {
    public static void main(String args[]) {
        int rows, i, j;
  
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of rows in pattern");
        rows = in.nextInt();

        for(i = 1; i <= rows; i++) {
            /* Prints one row of triangle */
            for(j = 1; j <= i; ++j) {
                System.out.print("* ");
            }
            /* move to next row */
            System.out.print("\n");
        }
    }
}
Output
Enter number of rows in pattern
6
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 

Recommended Posts
Java Program to Print Inverted Right Triangle Star Pattern
Java Program to Print Pyramid Pattern of Stars
Java program to Print Square Pattern of Star Character
Java Program to Print Right Triangle Pattern of Natural Numbers
Java Program to Print Diamond Pattern of Star Character
Java Program to Print Multiplication Table Triangle Pattern
Java Program to Print Inverted Pyramid Pattern of Stars
Java Program to Print Pascal Triangle
Java Program to Delete All Spaces from a String
Java Program to Check Whether two Strings are Equal or Not
Java Program to Check Two Strings are Anagram or Not
All Java Programs