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--;
}
}
} |
2 comments:
Above Complete code is copied from <a href="https://www.sitesbay.com/java-program/java-triangle-of-star:>Print Star Pattern in Java</a> please remove this code
Above Complete code is copied from Print Star Pattern in Java please remove this code
Post a Comment