- To print full triangle pattern using number
User Input: 3
Output:
1 1 2 1 1 2 3 2 1
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,k;
rows=userInput.nextInt();
for(int i=rows; i>=1; i--) {
k=1;
for(int j=1; j<=(2*rows-i); j++) {
if(j<i)System.out.print(" ");
else {
if(j<rows)System.out.print(k++ +" ");
else System.out.print(k-- +" ");
}
}
System.out.print("\n");
}
}
}
Enter number of rows: 4 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1Program
Main Logic :
for(int i=rows; i>=1; i--) {
k=1;
for(int j=1; j<=(2*rows-i); j++) {
if(j<i)System.out.print(" ");
else {
if(j<rows)System.out.print(k++ +" ");
else System.out.print(k-- +" ");
}
}
System.out.print("\n");
}
Concept
Coming Soon !
QuickIf your happiness depends on what somebody else does, I guess you do have a problem.