Tag: java pattern program

  • Square Number Pattern in Java: 2

    Square Number Pattern in Java: 2

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


    Number Square Pattern Programs in Java

    The programs below take the user input for the number of rows to be displayed in the pattern. We have used a for loop to iterate.

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

    Output:

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


  • 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

  • Inverted Hollow Pyramid using Stars in Java

    Inverted Hollow Pyramid using Stars in Java

    In this tutorial, we will write a program to print an inverted 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 inverted hollow triangle star pattern in java program.


    Inverted hollow pyramid star pattern in java

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int row, i, j;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        row = sc.nextInt();
    
        for (i = row; i > 0; i--)
        {
          for (j = 1; j <= row - i; j++)
            System.out.print(" ");
          if (i == 1 || i == row)
          {
            for (j = 1; j <= i *2 - 1; j++)
              System.out.print("*");
          }
          else
          {
            for (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:

    Inverted hollow pyramid star pattern in java

  • 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

  • Java String Pattern: 10

    Java String Pattern: 10

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

    String Pattern:

    public class Main
    {
      public static void main(String args[])
      {
        String str = "Simple2Code";
        int n = 6;
        int x = 0;
        int i, j, k;
    
        for (i = 0; i < n; i++)
        {
          for (j = 0; j < i; j++)
            System.out.print("  ");
    
          for (k = 0; k < n - i; k++)
          {
            System.out.print(str.charAt(x++) + " ");
    
            if (x == str.length())
              x = 0;
    
          }
    
          System.out.println();
        }
      }
    }

    Output:

    S i m p l e
      2 C o d e
        S i m p
          l e 2
            C o
              d

  • Java String Pattern: 9

    Java String Pattern: 9

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

    String Pattern:

    public class Main
    {
      public static void main(String args[])
      {
        String str = "SIMPLE2CODE";
        int n = 6;
        int x = 0;
        int i, j;
    
        for (i = 0; i < n; i++)
        {
          for (j = 0; j <= i; j++)
          {
            System.out.print(str.charAt(x++) + " ");
    
            if (x == str.length())
              x = 0;
    
          }
    
          System.out.println();
        }
      }
    }

    Output:

    S
    I M
    P L E
    2 C O D
    E S I M P
    L E 2 C O D


  • Java String Pattern: 8

    Java String Pattern: 8

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

    String Pattern:

    public class Main
    {
      public static void main(String args[])
      {
        String arr = "SIMPLE2CODE";
        int n = 6;
        int x = 0;
        int i, j, k;
    
        for (i = 0; i < n; i++)
        {
          for (j = n - 1; j > i; j--)
            System.out.print("  ");
    
          for (k = 0; k <= i; k++)
          {
            System.out.print(arr.charAt(x++) + " ");
            if (x == arr.length())
              x = 0;
          }
    
          System.out.println();
        }
      }
    }

    Output:

    S I M P L E
      S I M P L
        S I M P
          S I M
            S I
              S

  • Java String Pattern: 7

    Java String Pattern: 7

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

    String Pattern:

    public class Main
    {
      public static void main(String args[])
      {
        String str = "SIMPLE";
        int i, j, k;
        int length = str.length();
    
        for (i = 0; i < length; i++)
        {
          for (j = length - 1; j > i; j--)
          {
            System.out.print("  ");
          }
    
          for (k = 0; k <= i; k++)
          {
            System.out.print(str.charAt(k) + " ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    S I M P L E
      S I M P L
        S I M P
          S I M
            S I
              S

  • Java String Pattern: 6

    Java String Pattern: 6

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

    Character Pattern Programs in Java

    The program below takes a user input for a word from the user using Scanner class and prints that word in various patterns format in java. The following are the three alphabet patterns in java with string input.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int i, j;
        String str;
        char[] ch;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the string: ");
        str = sc.nextLine();
    
        ch = str.toCharArray();
        //upper part
        for (i = 0; i < ch.length; i++)
        {
          for (j = 0; j < i; j++)
            System.out.print(ch[j] + " ");
    
          System.out.println();
        }
        //lower part
        for (i = ch.length; i >= 0; i--)
        {
          for (j = 0; j < i; j++)
            System.out.print(ch[j] + " ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the string: SIMPLE

    S
    S I
    S I M
    S I M P
    S I M P L
    S I M P L E
    S I M P L
    S I M P
    S I M
    S I
    S