Pattern problem in java.
Need to display right angle triangle format with decreasing term based on the input.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Input
5
output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
5
output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Comments
Post a Comment