- To print half triangle pattern using number
User Input: 3
Output:
1 2 2 3 3 3
import java.util.Scanner;
public class Pattern {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter number of rows : ");
int rows;
rows=userInput.nextInt();
for(int i=1; i<=rows; i++) {
for(int j=1; j<=i; j++) {
System.out.print(i +" ");
}
System.out.print("\n");
}
}
}
Enter number of rows : 4 1 2 2 3 3 3 4 4 4 4Program
Main logic :
for(int i=1; i<=rows; i++) { //rows : number of rows to print
for(int j=1; j<=i; j++) {
System.out.print(i +" ");
}
System.out.print("\n");
}
Concept