10 different Number Pattern Programs in Java

java number pattern programs

 

Similar articles :

 

Pattern 1


Enter a number between 1 to 9 : 4
   1
  121
 12321
1234321

package com.topjavatutorial;

import java.util.Scanner;

public class NumberPattern {

  public static void main(String[] args) {
    int num, space;

    System.out.print("Enter a number between 1 to 9 : ");

    Scanner reader = new Scanner(System.in);
    try {
      num = reader.nextInt();

      space = num - 1;

      for (int i = 1; i <= num; i++) {
        for (space = 1; space <= (num - i); space++) {
          System.out.print(" ");
        }

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

        for (int k = (i - 1); k >= 1; k--) {
          System.out.print(k);
        }

        System.out.println();

      }
    } finally {
      reader.close();
    }
  }
}

Pattern 2


1
22
333
4444
55555

package com.topjavatutorial;

public class NumberPattern2 {

  public static void main(String[] args) {
    int count = 5;

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

}

Pattern 3


1
12
123
1234
12345
1234
123
12
1

package com.topjavatutorial;

public class NumberPattern3 {

  public static void main(String[] args) {
    int n = 5;

    for (int i = 1; i < n; i++) {
      for (int j = 1; j <= i; j++)
        System.out.print(j);
      System.out.println();
    }
    for (int i = n; i >= 0; i--) {
      for (int j = 1; j <= i; j++)
        System.out.print(j);
      System.out.println();
    }
    System.out.println();
  }
}

Pattern 4


12345
1234
123
12
1

1
12
123
1234
12345

package com.topjavatutorial;

public class NumberPattern4 {

  public static void main(String[] args) {
    int n = 5;

    for (int i = n; i >= 0; i--) {
      for (int j = 1; j <= i; j++)
        System.out.print(j);
      System.out.println();
    }

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

    System.out.println();
  }
}

Pattern 5


1
01
101
0101
10101

package com.topjavatutorial;

public class NumberPattern5 {

  public static void main(String[] args) {
     int n, p, q;
     
          n = 5;
          for (int i = 1; i <= n; i++)
          {
              if (i % 2 == 0)
              { p = 1; q = 0; }
              else
              { p = 0; q = 1; }
              for (int j = 1; j <= i; j++)
                  if (j % 2 == 0)
                      System.out.print(p);
                  else
                    System.out.print(q);
              System.out.println();
   
          }
  }
}

Pattern 6


1
23
456
78910

package com.topjavatutorial;

public class NumberPattern6 {

  public static void main(String[] args) {
    int rows, k = 1;
      
        rows = 4;
        for (int i = 1; i <= rows; i++)
        {
            for (int j = 1; j <= i; j++)
                System.out.print(k++);
            System.out.println();
        }  
  }
}

Pattern 7


    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 

package com.topjavatutorial;

public class NumberPattern7 {

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

Pattern 8


5
54
543
5432
54321

package com.topjavatutorial;

public class NumberPattern8 {

  public static void main(String[] args) {

    int i = 5;
    while (i >= 1) {
      int j = 5;
      while (j >= i) {
        System.out.print(j);
        j--;
      }
      i--;
      System.out.println();
    }
  }
}

Pattern 9


1****
12***
123**
1234*
12345

package com.topjavatutorial;

public class NumberPattern9 {

  public static void main(String[] args) {
    int i, j, k;
    int n = 5;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= i; ++j)
        System.out.print(j);

      for (k = n - i; k >= 1; k--)
        System.out.print("*");

      System.out.println();
    }

  }
}

Pattern 10


    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 
 1 2 3 4 
  1 2 3 
   1 2 
    1 

package com.topjavatutorial;

public class NumberPattern10 {

  public static void main(String[] args) {
    int i, j, k;
    
    for (i = 1; i <= 5; i++) {
      for (j = 1; j <= 5 - i; j++)
        System.out.print(" ");

      for (k = 1; k <= i; k++)
        System.out.print(k + " ");

      System.out.println();
    }

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

      for (k = 1; k <= 5 - i; k++)
        System.out.print(k + " ");

      System.out.println();
    }

  }
}


 

Pattern 11


1
21
321
4321
54321

public class JavaNumberPattern11 {
  public static void main(String[] args) {
    int n = 5; // Use Scanner as shown in previous examples if u want user to enter this

    for (int i = 1; i <= n; i++) {

      for (int k = i; k >= 1; k--) {
        System.out.print(k);
      }

      System.out.println();
    }

  }
}


 

© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

31 comments

  1. Gihan Lavindu

    Please help me to print this code…

    1
    2 2
    0 0 0
    1 1 1 1
    2 2 2 2 2
    0 0 0 0 0 0

  2. shubham jain

    how to make the pattern.
    1
    232
    34543
    and
    1
    23
    345
    4567

  3. How to print this pattern.

    9204605198
    920460519
    92046051
    9204605
    920460
    92046
    9204
    920
    92
    9

  4. manas sawant

    using for loop
    1111
    000
    11
    0

    print this pattern.

  5. Alternate solution for Pattern 5

    public class Pattern{
    public static void main(String[] args) {
    int n =5;
    for(int i=1;i<=n;i++)
    {
    for(int j=1;j<=i;j++)
    {
    if((i%2==0&&j%2==0)|(i%2!=0&&j%2!=0)) //if both i and j are even or both are odd then
    print 1
    System.out.print(1+" ");
    else
    System.out.print(0+" ");

    }

    System.out.println();
    }

    }
    }

  6. please help me in

    5 4 3 2 1
    4 3 2 1
    3 2 1
    2 1
    1

    1. void main()
      {
      for(int i=1;i=1;j++)
      {
      System.out.print(j);
      }
      System.out.println();
      }
      }

  7. Debashis Nanda

    Help me to print this pattern
    * * * * *
    * *
    * *
    * *
    * * * * *

  8. Debashis Nanda

    Help me to print this pattern in java
    * * * * * * *
    * * * * *
    * * *
    *
    * * *
    * * * * *
    * * * * * * *

    1. Vijay Kadam

      public class Pat70{

      public static void main(String []args){
      int x=5;

      for(int i=1;i5)x=x+2;
      for(int j=i;j<=x;j++)
      {System.out.print("*");
      }
      System.out.println();

      }

      }
      }

  9. help
    * *
    ** **
    *** ***
    **** ****
    **********

  10. can you help me with this program

    0
    1 0 1
    2 1 * 1 2
    1 0 1
    0

  11. 1
    121
    12321
    1234321
    123454321
    12345654321
    1234567654321
    12345654321
    123454321
    1234321
    12321
    121
    1
    please help me with this one 🙁

    1. Krishnan Mishra

      1
      121
      12321
      1234321
      123454321
      12345654321
      1234567654321
      12345654321
      123454321
      1234321
      12321
      121
      1

      class JavaApplication1{

      public static void main(String args[]) {
      for(int i=1;i<8;i++){
      int j=1;
      while(j1){
      System.out.print(–l);
      }
      System.out.println();
      }
      for(int i=6;i>0;i–){
      int j=1;
      while(j1)
      System.out.print(–l);
      System.out.println();
      }
      }
      }

  12. 0
    101
    21012
    3210123
    432101234
    send me code for this

    1. /*

      0
      101
      21012
      3210123
      432101234
      */
      import java.util.Scanner;
      public class Pattern
      {
      public static void main(String[] args)
      {
      Scanner scanner =new Scanner(System.in);
      System.out.println(“Enter No of rows : “);
      int n=scanner.nextInt();
      int k=0;
      boolean addCheck;
      for(int i=0;i<n;i++)
      {
      k=i;
      addCheck=false;
      for(int j=0;j0)
      {
      System.out.print(k);
      if(!addCheck)
      {
      k–;
      }
      else
      {
      k++;
      }
      }
      else if(k==0)
      {
      System.out.print(k);
      addCheck=true;
      k++;
      }
      }
      System.out.println();
      }
      }
      }

  13. SAURABH KUMAR YADAV

    Help me print this pattern
    12345
    21234
    32123
    43212
    54321

    1. Krishnan Mishra

      /*
      1 2 3 4 5
      2 1 2 3 4
      3 2 1 2 3
      4 3 2 1 2
      */

      class JavaApplication1{

      public static void main(String args[]) {
      for(int i=1;i<5;i++){
      for(int j=1,k=i,l=1,m=5-i;j0){
      System.out.print(k–+” “);
      }
      while(m>0){
      System.out.print(++l +” “);
      m–;
      }
      }
      System.out.println();
      }
      }
      }

  14. saptaparna basu

    help me
    0
    1 2
    3 4 5
    9 8 7 6
    5 4 3
    2 1
    0

  15. help to this one/

    1
    212
    32123
    4321234
    543212345
    65432123456
    7654321234567
    65432123456
    543212345
    4321234
    32123
    212
    1

  16. 1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5

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

  17. Honey Villamor

    This one?
    1
    2 1 2
    3 2 1 2 3
    4 3 2 1 2 3 4
    5 4 3 2 1 2 3 4 5
    4 3 2 1 2 3 4
    3 2 1 2 3
    2 1 2
    1

    Thanks.

    1. ye program bna k nhi bhai

    2. Krishnan Mishra

      /*
      1
      2 1 2
      3 2 1 2 3
      4 3 2 1 2 3 4
      5 4 3 2 1 2 3 4 5
      4 3 2 1 2 3 4
      3 2 1 2 3
      2 1 2
      1
      */

      class JavaApplication1{

      public static void main(String args[]) {
      for(int i=1;i<6;i++){
      for(int j=1,k=i,l=1;j0){
      System.out.print(k–+” “);
      }
      while(l0;i–){
      for(int j=1,k=i,l=1;j0){
      System.out.print(k–+” “);
      }
      while(l<i){
      System.out.print(++l +" ");
      }
      }
      System.out.println();
      }
      }
      }

  18. sangeetha

    help me for this pattern
    1
    3 2
    6 5 4
    10 9 8 7
    15 14 13 12 11

    1. Krishnan Mishra

      1
      3 2
      6 5 4
      10 9 8 7
      15 14 13 12 11

      class JavaApplication1{

      public static void main(String args[]) {
      for(int i=1;i0){
      System.out.print(s– + ” “);
      j–;
      }
      System.out.println();
      }
      }
      }

  19. how to print this patterns
    1
    11
    121
    1331

    and
    #
    #*
    #*#
    #*#*
    #*#*#
    please help

    1. #
      #*
      #*#
      #*#*
      #*#*#
      // the code is here

      class Main
      {
      public static void main(String args[])
      {
      int n=5;
      for(int i=0;i<n;i++)
      {
      for(int j=0;j<=i;j++)
      {
      if(j%2==0)
      System.out.print("#");
      else
      System.out.print("*");
      }
      System.out.println();
      }
      }
      }

  20. S .Pranav Balaji

    Help me print this pattern

    1 2 3 4 5 4 3 2 1
    1 2 3 4 * 4 3 2 1
    1 2 3 * * * 3 2 1
    1 2 * * * * * 2 1
    1 * * * * * * * 1

  21. Matej Rastocky

    And pytagoras triangle ? 😮

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: