Creating Number Patterns In Java

Posted by Somesh Shinde On Saturday, 30 July 2016 2 comments

Creating Number Patterns In Java


Here are some common number patterns and Related Logic:

Pattern 1. 
Pattern 1
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=1;j<=i;j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}
Pattern 2. 
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. 
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. 
Pattern 4
To Print this pattern use the following code:
public class numPtr {
    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. 

Pattern 5
To Print this pattern use the following code:
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:

hitesh kumar said...

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

hitesh kumar said...

Above Complete code is copied from Print Star Pattern in Java please remove this code

Post a Comment