In this article, we are going to print java code to print triangle pattern.
Example if n=4.
Steps:-
- Take the input for number of lines in a triangle, from the user using scanner.
- Run a loop for number of lines.
- Inside the previous loop run another loop for printing * which equals to line’s number.
java code to print triangle pattern:-
package gangforcode;
import java.util.Scanner;
public class TrianglePattern {
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<=i) {
System.out.print("*");
j++;
}
i++;
System.out.println();
}
}
}
Output:-
you can use same logic for printing triangle pattern in other programming languages like C,C++,Pyhton etc.
Thanks!!!!