Pattern Program in Java


Today I am going to explain you some pattern program in Java.

Que 1: Write a program to print isosceles triangle in Java?

public class PrintIsocscelesTriangle {

    public static void main(String[] args) {
        int row, col, space;
      for(row = 1; row <= 20; row+=2)
      {

              for(space=18; space >= row; space-=2)
                      {
                              System.out.print(' ');
                      }
             
              for(col = 1; col <=(row); col++ )
              {
                  System.out.print("*");
              }
          System.out.println("");
      }
    }
}


Output:                                               
                                                             
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************



Que 2: Write a program to print the below pattern in Java?


1  2 
1  2  3 
1  2  3  4 
1  2  3  4  5 
1  2  3  4  5  6 
1  2  3  4  5  6  7 
1  2  3  4  5  6  7  8 
1  2  3  4  5  6  7  8  9
1  2  3  4  5  6  7  8  9  10

public class Pattern1 {

    public static void main( String arg[]){
    int i,j;
  
        for( i=1;i<=10;i++){

            for( j=1;j<=i;j++){
                System.out.print(j+"  ");
            }
            System.out.println();
           }
     } 

}




Que 3: Write a program to print the below pattern in Java?

1  2  3  4  5  6  7  8  9  10 
1  2  3  4  5  6  7  8  9 
1  2  3  4  5  6  7  8 
1  2  3  4  5  6  7 
1  2  3  4  5  6 
1  2  3  4  5 
1  2  3  4 
1  2  3 
1  2 
1

public class Pattern2 {
    public static void main(String[] args) {
        int i,j;
         for( i=10;i>=1;i--){
            for(j=1;j<=i;j++){
                System.out.print(j + "  ");
            }
            System.out.println();        }
    }
}




Que 4: Write a program to print the below pattern in Java?


1  2 
1  2  3 
1  2  3  4 
1  2  3  4  5 
1  2  3  4  5  6 
1  2  3  4  5  6  7 
1  2  3  4  5  6  7  8 
1  2  3  4  5  6  7  8  9 
1  2  3  4  5  6  7  8  9  10 
1  2  3  4  5  6  7  8  9 
1  2  3  4  5  6  7  8 
1  2  3  4  5  6  7 
1  2  3  4  5  6 
1  2  3  4  5 
1  2  3  4 
1  2  3 
1  2 
1

public class Pattern3 {
    public static void main(String[] args) {
        int i,j;
  
        for( i=1;i<=10;i++){

            for( j=1;j<=i;j++){
                System.out.print(j+"  ");
            }
            System.out.println();
       }
        for( i=9;i>=1;i--){
            for(j=1;j<=i;j++){
                System.out.print(j + "  ");
            }
            System.out.println();        

        }
      }

}



Que 5: Write a program to print the below pattern in Java?

1  2  3  4  5  6  7  8  9  10 
1  2  3  4  5  6  7  8  9 
1  2  3  4  5  6  7  8 
1  2  3  4  5  6  7 
1  2  3  4  5  6 
1  2  3  4  5 
1  2  3  4 
1  2  3 
1  2 


1  2 
1  2  3 
1  2  3  4 
1  2  3  4  5 
1  2  3  4  5  6 
1  2  3  4  5  6  7 
1  2  3  4  5  6  7  8 
1  2  3  4  5  6  7  8  9 
1  2  3  4  5  6  7  8  9  10   


public class Pattern3 {
    public static void main(String[] args) {
        int i,j;
  
       
        for( i=10;i>=1;i--){
            for(j=1;j<=i;j++){
                System.out.print(j + "  ");
            }
            System.out.println();        }
        for( i=1;i<=10;i++){

            for( j=1;j<=i;j++){
                System.out.print(j+"  ");
            }
            System.out.println();
       }
      }
    }




 Que 6: Write a program to print the below pattern in Java?

*********
 *******
  *****
   ***
    *


public class Pattern4 {
    public static void main(String[] args) {  
        int n=5;       
            for(int i=1;i<=n;i++){
                for(int j=i;j>1;j--){
                    System.out.print(" ");
                }
                for(int k=1;k<=n-(i-1);k++){
                    System.out.print("*");
                    for(int k1=1;k1<k;k1+=k){
                        System.out.print("*");
                    }
                }
                System.out.println();     
                }
      } 
 }




 Que 7: Write a program to print the below pattern in Java?
    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    * 

public class Pattern4 {
    public static void main(String[] args) {
        int n=5;
            for(int i=n;i>=1;i--){
                for(int j=i;j>1;j--){
                    System.out.print(" ");
                }
                for(int k=1;k<=n-(i-1);k++){
                    System.out.print("*");
                    for(int k1=1;k1<k;k1+=k){
                        System.out.print("*");
                    }
                }
                System.out.println();     
            }
            for(int i=1;i<=n;i++){
                for(int j=i;j>1;j--){
                    System.out.print(" ");
                }
                for(int k=1;k<=n-(i-1);k++){
                    System.out.print("*");
                    for(int k1=1;k1<k;k1+=k){
                        System.out.print("*");
                    }
                }
                System.out.println();     
            }
    }
}




Note: If you have any problem then just drop me a comment. 

Post a Comment

1 Comments

  1. Well, these with numbers appear to be pretty easy, but I stuck on those with symbols. If I get this clear, is it sort of common questions for Java Dev job interviews? I'm new-new to Java, have started just several months ago with some online courses, but it went out pointless to me, because they're too much rapid. Now I stand with this resource in order to get to know all the basics of java core more details you may see here. Now I have enough confidence in order to write simple solutions and programs by the book, but some practice is required in order to sort out questions like these with any manuals

    ReplyDelete