if input is::4
4 4 4 4
3 3 3
2 2
1
2 2
3 3 3
4 4 4 4
program::
public class Comment {
public static void main( String arg[]){
int input = 4; //change it accordingly
for(int i=1 ; i<=input ; i++){
for(int j=1 ; j<=input-i+1 ; j++){
System.out.print(input-i+1+" ");
}
System.out.println();
}
for(int k=input-1; k>0 ; k--){
for(int l=2 ; l<=input-k+2 ; l++){
System.out.print(input-k+1+" ");
}
System.out.println();
}
}
}
public static void main( String arg[]){
int input = 4; //change it accordingly
for(int i=1 ; i<=input ; i++){
for(int j=1 ; j<=input-i+1 ; j++){
System.out.print(input-i+1+" ");
}
System.out.println();
}
for(int k=input-1; k>0 ; k--){
for(int l=2 ; l<=input-k+2 ; l++){
System.out.print(input-k+1+" ");
}
System.out.println();
}
}
}
Creating Number Patterns In Java
Here are some common number patterns and Related Logic:
public class numPtr {
public static void main( String arg[]){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
Pattern 2.
To Print this pattern use the following code:
public class numPtr {
public static void main( String arg[]){
for(int i=1;i<=5;i++){
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println();
}
}
}
|
Pattern 3.
To Print this pattern use the following code:
public class numPtr {
public static void main( String arg[]){
for(int i=1,r=5;i<=5;i++,r--){
for(int j=1;j<=r;j++){
System.out.print(j);
}
System.out.println(); }
}
}
Pattern 4.
public static void main( String arg[]){
int ck=0,c=2;
while(c>0){
if(ck==0){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println(); }
ck++;
} else{
for(int i=1,r=5-1;i<=5-1;i++,r--){
for(int j=1;j<=r;j++){
System.out.print(j); }
System.out.println();
}
}
c--;
}
}
}
Pattern 5.
public class numPtr {
public static void main( String arg[]){
int ck=0,c=2;
while(c>0){
if(ck==0){
for(int i=1,r=5;i<=5;i++,r--){
for(int j=1;j<=r;j++){
System.out.print(j);
}
System.out.println(); }
ck++;
} else{
for(int i=2;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j); }
System.out.println();
}
}
c--;
}
}
} |
Printing Star Patterns In Java
In Java language you can print triangle shape using for loop and also using while loop, here i will show you in simple way to print triangle.
Print star pattern in java
class StarTriangle { public static void main(String[] args) { int i,j,k; for(i=1; i<=5; i++) { for(j=4; j>=i; j--) { System.out.print(" "); } for(k=1; k<=(2*i-1); k++) { System.out.print("*"); } System.out.println(""); } } }
Output
* * * * * * * * * * * * * * *
Syntax to compile and run java program
Syntax
for compile -> c:/>javac StarTriangle.java for run -> c:/>java StarTriangle
Explanation of Code
- System.out.print("....."): are used for display message on screen or console but cursor not move in new line.
- System.out.println("....."): are used for display message on screen or console cursor move in new line.
Example
class Star { public static void main(String[] args) { int i, j, k; for(i=5;i>=1;i--) { for(j=5;j>i;j--) { System.out.print(" "); } for(k=1;k<(i*2);k++) { System.out.print("*"); } System.out.println(); } } }
Output
* * * * * * * * * * * * * * * * * * * * * * * * *
Example
class Star { public static void main(String[] args) { int i,j; for(i=1; i<=6; i++) { for(j=1; j<i; j++) { System.out.print("*"); } System.out.println(); } } }
Output
* * * * * * * * * * * * * * *
Example
class Star { public static void main(String[] args) { int i, j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { System.out.print("*"); } System.out.println(); } } }
Output
* * * * * * * * * * * * * * *
Example
class Star { public static void main(String[] args) { int i, j, k; for(i=5;igt;=1;i--) { for(j=1;jlt;i;j++) { System.out.print(" "); } for(k=5;k>=i;k--) { System.out.print("*"); } System.out.println(); } } }
Output
* * * * * * * * * * * * * * *
Example
class Star { public static void main(String[] args) { int i, j, k; for(i=5;i>=1;i--) { for(j=5;j>i;j--) { System.out.print(" "); } for(k=1;k<=i;k++) { System.out.print("*"); } System.out.println(); } } }
Output
* * * * * * * * * * * * * * *
Example
class Star { public static void main(String[] args) { int i, j, k; for(i=1;i<=5;i++) { for(j=i;j<5;j++) { System.out.print(" "); } for(k=1;k<(i*2);k++) { System.out.print("*"); } System.out.println(); } for(i=4;i>=1;i--) { for(j=5;j>i;j--) { System.out.print(" "); } for(k=1;k<(i*2);k++) { System.out.print("*"); } System.out.println(); } } }
Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *