Tag: java pattern program

  • Alphabet Pyramid Pattern in Java: pattern 3

    Alphabet Pyramid Pattern in Java: pattern 3

    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.

    Trianglular alphabet pattern

    This is a program the pattern is that the alphabet starts with A and at every increment of the row there is an increment in alphabet such as A, AB, ABC, and so on.

    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 = 1; i <= rows; i++)
        {
          for (j = 1; j <= rows - i; j++)
            System.out.print(" ");
    
          for (j = 1; j <= i; j++)
            System.out.print((char)(char)(j + 64) + " ");
    
          System.out.println();
        }
      }
    }
    Enter the number of rows: 7
    Output:
          A
         A B
        A B C
       A B C D
      A B C D E
     A B C D E F
    A B C D E F G

  • Alphabet Pyramid Pattern in Java: pattern 2

    Alphabet Pyramid Pattern in Java: pattern 2

    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.

    Trianglular alphabet pattern

    This is a program the right half and left half remain exactly the same on each row forming a triangular pyramid.

    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\n");
        for (i = 1; i <= rows; i++)
        {
          for (j = 1; j <= rows - i; j++)
            System.out.print(" ");
    
          //left half
          for (j = i; j > 0; j--)
            System.out.print((char)(j + 64));
    
          //right half
          for (j = 2; j <= i; j++)
            System.out.print((char)(j + 64));
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the number of rows: 8
    Output:
         
           A
          BAB
         CBABC
        DCBABCD 
       EDCBABCDE
      FEDCBABCDEF
     GFEDCBABCDEFG
    HGFEDCBABCDEFGH

  • Square Number Pattern in Java: 10

    Square Number Pattern in Java: 10

    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 <= 5; i++)
        {
          for (j = 1; j <= 5; j++)
          {
            if (j <= i)
              System.out.print(i + " ");
            else
              System.out.print(j + " ");
          }
          System.out.println();
        }
      }
    }

    Output:

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


  • Square Number Pattern in Java: 9

    Square Number Pattern in Java: 9

    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, k;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        for (i = 1; i <= rows; i++)
        {
          j = (i *2) - 1;
    
          for (k = i; k <= rows; k++)
          {
            System.out.print(j + " ");
            j += 2;
          }
    
          for (j = (i *2) - 3; j >= 1; j -= 2)
            System.out.print(j + " ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    1 3 5 7 9
    3 5 7 9 1
    5 7 9 3 1
    7 9 5 3 1
    9 7 5 3 1


  • Square Number Pattern in Java: 8

    Square Number Pattern in Java: 8

    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, k;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        for (i = 1; i <= rows; i++)
        {
          j = (i *2) - 1;
    
          for (k = 1; k <= rows; k++)
          {
            System.out.print(j + " ");
            j += 2;
    
            if (j > (rows *2) - 1)
              j = 1;
          }
          System.out.println();
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    1 3 5 7 9
    3 5 7 9 1
    5 7 9 1 3
    7 9 1 3 5
    9 1 3 5 7


  • Square Number Pattern in Java: 7

    Square Number Pattern in Java: 7

    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 = i; j <= rows; j++)
            System.out.print(j + " ");
    
          for (j = i - 1; j >= 1; j--)
            System.out.print(j + " ");
    
          System.out.println();
        }
      }
    }

    Output:

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


  • Square Number Pattern in Java: 6

    Square Number Pattern in Java: 6

    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, k;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        for (i = 1; i <= rows; i++)
        {
          j = i;
    
          for (k = 1; k <= rows; k++)
          {
            System.out.print(j + " ");
            j++;
    
            if (j > rows)
              j = 1;
          }
          System.out.println();
        }
      }
    }

    Output:

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


  • Square Number Pattern in Java: 5

    Square Number Pattern in Java: 5

    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, count = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        for (i = rows; i >= 1; i--)
        {
          for (j = rows; j >= i; j--)
          {
            System.out.print(j + " ");
            count = j;
          }
    
          for (j = (rows - i + 1); j < rows; j++)
            System.out.print(count + " ");
    
          System.out.println();
        }
      }
    }

    Output:

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


  • Square Number Pattern in Java: 4

    Square Number Pattern in Java: 4

    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 = 0; i < rows; i++)
        {
          for (j = 0; j < rows; j++)
          {
            if (j % 2 == 0)
              System.out.print((rows *(j)) + i + 1 + "  ");
            else
              System.out.print((rows *(j + 1)) - i + "  ");
          }
          System.out.print("\n");
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    1 10 11 20 21
    2 9 12 19 22
    3 8 13 18 23
    4 7 14 17 24
    5 6 15 16 25


  • Square Number Pattern in Java: 3

    Square Number Pattern in Java: 3

    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 = rows; i >= 1; i--)
        {
          for (j = i; j < rows; j++)
            System.out.print(j + " ");
    
          for (j = rows - i; j < rows; j++)
            System.out.print(5 + " ");
    
          System.out.println();
        }
      }
    }

    Output:

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