Here is 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++) { for(j = 1; j <= i; ++j) { System.out.print("* "); } System.out.print("\n"); } } }Output
Enter number of rows in pattern 6 * * * * * * * * * * * * * * * * * * * * *
Recommended Posts