Tag: java pattern program

  • Reverse Pyramid with Alphabet in Java

    Reverse Pyramid with Alphabet in Java

    In this tutorial, we will write a reverse alphabet triangle pattern in java. Before that, you may go through the following topic in java.

    The program takes a user input for the number of rows to be printed. Then the inverted alphabet pattern is displayed on the screen.


    Java Program to display the Reverse Pyramid pattern with Alphabet

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = scan.nextInt();
    
        System.out.print("Output:\n");
        for (i = rows; i >= 1; i--)
        {
          for (j = 1; j <= rows - i; j++)
            System.out.print("  ");
    
          for (j = 1; j <= 2 *i - 1; j++)
          {
            if (j <= i)
              System.out.print((char)(char)(j + 64) + " ");
            else
              System.out.print((char)(char)(2 *i - j + 64) + " ");
          }
          System.out.println();
        }
      }
    }
    Enter the number of rows: 5
    Output:
    A B C D E D C B A
      A B C D C B A
        A B C B A
          A B A
            A

  • Alphabet Pyramid Pattern in Java: pattern 1

    Alphabet Pyramid Pattern in Java: pattern 1

    In this tutorial, we will write a Java program to display alphabet pyramid patterns. Before that, you may go through the following topic in java.

    We will look at pyramid patterns in java using alphabets/letters/characters.

    Pyramid triangle alphabet

    This is a mirror image pattern of letters in java.

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

    Enter the number of rows: 6
         A
        ABA
       ABCBA
      ABCDCBA
     ABCDEDCBA
    ABCDEFEDCBA
  • Hourglass Star Pattern in Java

    Hourglass Star Pattern in Java

    In this tutorial, we will write a java program to print sandglass star patterns. 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 a star.

    Input of row: 4  
    * * * *
     * * * 
      * * 
       *  
       * 
      * * 
     * * * 
    * * * *

    Hourglass Star 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();
    
        System.out.print("Output: \n\n");
        //upper half
        for (i = 1; i <= rows; i++)
        {
          for (k = 1; k <= i - 1; k++)
          {
            System.out.print("*" + " ");
          }
          for (j = 1; j <= rows - i + 1; j++)
          {
            System.out.print("*");
          }
          System.out.println();
        }
    
        //lower half
        for (i = rows - 1; i >= 1; i--)
        {
          for (k = 1; k <= i - 1; k++)
          {
            System.out.print(" ");
          }
          for (j = i; j <= rows; j++)
          {
            System.out.print("*" + " ");
          }
          System.out.println();
        }
      }
    }

    Output:

    Hourglass Star 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


  • Hollow Pyramid using Stars in Java

    Hollow Pyramid using Stars in Java

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

    Both of the programs below take the user input for the number of rows and display the result using the for loop in java. The program is basically the hollow triangle star pattern in java program.


    Hollow Pyramid using Stars 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();
    
        for (int i = 1; i <= n; i++)
        {
          for (int j = 1; j <= n - i; j++)
          {
            System.out.print(" ");
          }
    
          if (i == 1 || i == n)
          {
            for (int j = 1; j <= i *2 - 1; j++)
            {
              System.out.print("*");
            }
          }
          else
          {
            for (int j = 1; j <= i *2 - 1; j++)
            {
              if (j == 1 || j == i *2 - 1)
                System.out.print("*");
              else
                System.out.print(" ");
            }
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Hollow Pyramid using Stars in Java

  • 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