Triangle pattern program using java

In this article, we are going to print java code to print triangle pattern.

Example if n=4.

Triangle pattern using java

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:-

java program to print triangle pattern

you can use same logic for printing triangle pattern in other programming languages like C,C++,Pyhton etc.

Thanks!!!!

Leave a Comment