Here is a Java program to print inverted(upside down)right angled triangle star pattern. An Inverted Right triangle star pattern of N rows contains (N-i+1) space separated '*' characters in ith row. Number of star characters in a row keeps on decreasing as as row number increases. 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-i+1) '*' characters for ith row.
Sample Optput * * * * * * * * * * * * * * * * * * * * *
Algorithm to print inverted right triangle star pattern
If you look closely, this pattern is the vertically inverted pattern of right triangle star pattern.
If you look closely, this pattern is the vertically inverted pattern of right triangle star pattern.
- Take the number of rows of inverted right triangle as input from user and store it in an integer variable.
- Number of stars in Kth row is equal to (N - K + 1).
- We will use two for loops as follows.
- Outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
- For Kth row, inner loop will iterate N-K+1 times. Each iteration will print one star (*) character.
Java program to print inverted triangle star pattern
package com.tcc.java.programs; import java.util.*; public class InvertedRightTrianglePattern { 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 = rows; i > 0; i--) { /* Prints one row of triangle */ for(j = i; j > 0; j--) { System.out.print("* "); } /* move to next row */ System.out.print("\n"); } } }Output
Enter number of rows in pattern 6 * * * * * * * * * * * * * * * * * * * * *
Recommended Posts