Tag: java number pattern

  • Inverted Half Pyramid using Numbers in Java

    Inverted Half Pyramid using Numbers in Java

    In this tutorial, we will write a number pattern program in java. The patterns are half pyramid patterns with different combinations of numbers such as in increasing order, decreasing order, or more.

    Before that, you may go through the following topic in java.

    Inverted Half Pyramid using Numbers in Java

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows;
    
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        for (int i = rows; i >= 1; --i)
        {
          for (int j = 1; j <= i; ++j)
          {
            System.out.print(j + " ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1

  • Java Number Pattern (Hill shape 1)

    Java Number Pattern (Hill shape 1)

    In this tutorial, we will go through the hill shape numeric pattern program in java. Before that, you may go through the following topic in java.

    Number Pattern:

    public class Main
    {
      public static void main(String args[])
      {
        int row = 5;
        int x = row;
        int y = row;
        int i, j;
    
        for (i = 1; i <= row; i++)
        {
          for (j = 1; j <= row * 2; j++)
          {
            if (j == x || j == y)
              System.out.print(i);
            else
              System.out.print(" ");
          }
    
          x--;
          y++;
    
          System.out.println();
        }
      }
    }

    Output:

            1
          2   2
        3       3
      4           4
    5               5

  • Java Number Pattern (Hill shape 2)

    Java Number Pattern (Hill shape 2)

    In this tutorial, we will go through the hill shape numeric pattern program in java. Before that, you may go through the following topic in java.

    Number Pattern:

    public class Main
    {
      public static void main(String args[])
      {
        int row = 5;
        int x = row;
        int y = row;
        int i, j;
    
        for (i = 1; i <= row; i++)
        {
          for (j = 1; j <= row * 2; j++)
          {
            if (j == x || j == y)
              System.out.print(x);
            else
              System.out.print(" ");
          }
    
          x--;
          y++;
    
          System.out.println();
        }
      }
    }

    Output:

            5
          4   4
        3       3
      2           2
    1               1

  • Inverted Hourglass Pattern in Java using Numbers

    Inverted Hourglass Pattern in Java using Numbers

    In this tutorial, we will write a java program to print the inverted hourglass pattern using numbers. Before that, you may go through the following topic in java.


    Inverted Hourglass Pattern in Java

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        int n = sc.nextInt();
        int nsp = 2 *n - 1;
    
        int nr = 2 *n + 1;
        int num = n;
        int a = 0;
    
        for (int i = 1; i <= nr; i++)
        {
          if (i > n + 1)
            a = nr - i + 1;
          else
            a = i;
    
          //numbers
          for (int cst = 1; cst <= a; cst++)
          {
            System.out.print(num);
            num--;
          }
    
          //Spaces
          for (int csp = 1; csp <= nsp; csp++)
          {
            System.out.print(" ");
          }
    
          for (int cst = 1; cst <= a; cst++)
          {
            num++;
            if (num != 0)
              System.out.print(num);
          }
    
          if (i <= (nr) / 2)
            nsp -= 2;
          else
            nsp += 2;
    
          System.out.println();
        }
      }
    }

    Output:

    Inverted Hourglass Pattern in Java

  • Hourglass Number Pattern in Java

    Hourglass Number Pattern in Java

    In this tutorial, we will write a java program to print sandglass number pattern. Before that, you may go through the following topic in java.

    The program below takes a user input for the number of rows needed and then in decreasing order of the first half is printed and then the increasing order of the second half of the sandglass is printed.

    This is an hourglass pattern program in java or you can say sandglass pattern program using number.


    Hourglass Number Pattern in Java

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows, i, j, k;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        //upper half
        for (i = 1; i <= rows; i++)
        {
          for (k = 1; k < i; k++)
            System.out.print(" ");
    
          for (j = i; j <= rows; j++)
            System.out.print(j + " ");
    
          System.out.println();
        }
    
        //lower half
        for (i = rows - 1; i >= 1; i--)
        {
          for (k = 1; k < i; k++)
            System.out.print(" ");
    
          for (j = i; j <= rows; j++)
            System.out.print(j + " ");
    
          System.out.println();
        }
      }
    }

    Output:

    Hourglass Number Pattern in Java

  • Mirror Image Triangular Number Pattern in Java

    Mirror Image Triangular Number Pattern in Java

    In this tutorial, we will write a program to print a triangle pattern in java where the pattern is repeated by dividing each row in two half just like a mirror. Before that, you may go through the following topic in java.

    Example:

    Input: 5
    Output:
         0
        101
       21012
      3210123
     432101234

    The program takes the user input for the number of rows and displays the result using the for loop in java. The program is a mirror triangle number pattern in java or you can say full pyramid of numbers with mirror image numbers.


    Program for triangular pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows, num1 = 1, num2 = 1;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        for (int i = 0; i < rows; i++)
        {
          for (int j = rows - 1; j > i; j--)
          {
            System.out.print(" ");
          }
    
          for (int k = 1; k <= num1; k++)
          {
            System.out.print(Math.abs(k - num2));
          }
    
          num1 += 2;
          num2++;
          System.out.println();
        }
      }
    }

    Output:

    Enter the number of rows: 6
         0
        101
       21012
      3210123
     432101234
    54321012345

  • Right Triangle Pattern of Numbers in Java with Repeating Pattern (Mirror image)

    Right Triangle Pattern of Numbers in Java with Repeating Pattern (Mirror image)

    In this tutorial, we will write a program to print a triangle pattern in java where the pattern is repeated on each row and keep on increasing. Before that, you may go through the following topic in java.

    Example:

    Input: 5
    1
    1 2 1
    1 2 3 2 1
    1 2 3 4 3 2 1

    The program takes the user input for the number of rows and displays the result using the for loop in java. The program is mirror right angled triangle pattern with a repetition pattern or you can say half pyramid of numbers with a repetition pattern.


    Java Program to print the right triangle number pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        for (int i = 1; i <= rows; i++)
        {
          //first half of the row            
          for (int j = 1; j <= i; j++)
            System.out.print(j + " ");
    
          //second half of the row              
          for (int j = i - 1; j >= 1; j--)
            System.out.print(j + " ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the number of rows: 5
    1
    1 2 1
    1 2 3 2 1
    1 2 3 4 3 2 1
    1 2 3 4 5 4 3 2 1

    1 2 3 4 5 6 5 4 3 2 1


  • Inverted Triangle Pattern in Java with Descending Order

    Inverted Triangle Pattern in Java with Descending Order

    In this tutorial, we will write a java program to print reverse right angled triangle of numbers with numbers in descending order. Before that, you may go through the following topic in java.

    The program takes the user input for the number of rows and displays the result using the for loop in java. The number is displayed by decreasing order until it becomes a single digit at the last.

    The program is an inverted right triangle number pattern in java or you can say half pyramid of numbers in decreasing order.


    Java program to print the right triangle pattern of Numbers

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        for (int i = 1; i <= rows; i++)
        {
          for (int j = rows; j >= i; j--)
          {
            System.out.print(j + " ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the number of rows: 8
    8 7 6 5 4 3 2 1
    8 7 6 5 4 3 2
    8 7 6 5 4 3
    8 7 6 5 4
    8 7 6 5
    8 7 6
    8 7
    8


  • Triangle Pattern with Repeated Digits in Java

    Triangle Pattern with Repeated Digits in Java

    In this tutorial, we will write a program to print triangular number patterns in java where the digits are repeated on each row. Before that, you may go through the following topic in java.

    The program takes the user input for the number of rows and displays the result using the for loop in java. The program is right angled triangle pattern or you can say half pyramid of numbers with repetition of digits.


    Java program to print right triangle pattern with repeatation of digits

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        for (int i = 1; i <= rows; i++)
        {
          for (int j = 1; j <= i; j++)
          {
            System.out.print(i + " ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the number of rows: 6
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5
    6 6 6 6 6 6


  • Java Program to print Half Pyramid using Numbers

    Java Program to print Half Pyramid using Numbers

    In this tutorial, we will write a number pattern program in java. The patterns are half pyramid patterns with different combinations of numbers such as in increasing order, decreasing order, or more.

    Before that, you may go through the following topic in java.


    Java Program to print Half Pyramid using Numbers

    The programs below take the user input for the number of rows to be printed for the half pyramid. Now let us go through the java program to print pyramid pattern of numbers.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows;
    
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        for (int i = 1; i <= rows; ++i)
        {
          for (int j = 1; j <= i; ++j)
          {
            System.out.print(j + " ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5