java basic patterns
here i'm providing basic of patterns in java programming those student are learning java so please do this pattern these are very helpfull for you all
1 code :
for (int i = 1; i<=5; i++){
for(int j = 1; j<=i; j++){
System.out.print(j);
}
System.out.println();
Output:
1
12
123
1234
12345
2 code :
for (int i = 1; i<=5; i++){
for(int j = 1; j<=i; j++){
System.out.print(i);
}
System.out.println();
}
Output :
1
22
333
4444
55555
3 Code :
for (int i = 1; i<=5; i++){
for(int j = 5; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
Output :
*****
****
***
**
*
4 Code :
for (int i = 5; i>=1; i--){
for(int j = 1; j<=i; j++){
System.out.print(j);
}
System.out.println();
}
Output:
12345
1234
123
12
1
5 Code :
int n = 5;
for (int i = 0; i<n; i++){
for(int j = 0; j<(n-i-1); j++){
System.out.print(" ");
}
for(int j = 0; j<(2*i+1); j++){
System.out.print("*");
}
for(int j = 0; j<(n-i-1); j++){
System.out.print(" ");
}
System.out.println();
}
Output :
*
***
*****
*******
*********