In this article we going to print inverted triangle pattern using java code.
Example:- if n=4 (n is the number of lines in triangle.)
Steps:-
- Take input from the user for the height of the triangle using a scanner.
- Run a loop for all the rows in the triangle.
- Inside loop run another loop for printing space.
- Inside the first loop run another loop for printing *.
Java code to print inverted triangle pattern
package gangforcode;
import java.util.*;
public class InvertedTriangle {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
int i=1;
while(i<=n) {
int j=1;
while(j<=(n-i)) {
System.out.print(" ");
j++;
}
while(j<=n) {
System.out.print("*");
j++;
}
i++;
System.out.println();
}
}
}
Output:-
You can use the same logic to print inverted triangle patterns in all other programming languages.