Pattern problem in java.

Need to display right angle triangle format with decreasing term based on the input.



import java.util.*;
public class PatternProblem {
public static void main(String [] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int num=n;
String number=String.valueOf(num);
int len=number.length();
for(int i=1;i<=n;i++){
for(int j=1;j<num*len+(num-len);j++){
System.out.print(" ");
}
num--;
int space=0; //this variable decides how many space to be left.
for(int k=1;k<i;k++){
if(i<10){
space=len;
}else if(i>=10 && i<100){
space=len-1;
}else if(i>=100 && i<1000){
space=len-2;
}
System.out.print(i);
for(int f=1;f<=space;f++){
System.out.print(" ");
}
}
System.out.println(i);
}
in.close();
}
}
view raw PatternProblem hosted with ❤ by GitHub

Input
5

output:
            1
         2 2
      3 3 3
   4 4 4 4
5 5 5 5 5

Comments

Popular posts from this blog

Reasoning-Number Series

Profit and Loss

Reasoning-Letter Series