Pattern Programs in Java


Pattern Programs in java

Pattern 1


class Pattern1
{
    public static void main(String args[])
    {
		int r = 5;
        for (int m = 1; m <= r; m++)
        {
            for (int n = 1; n <= m; n++)
            {
                System.out.print(n + " ");
            }
            System.out.println();
        }
    }
}

Output

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

Pattern 2


class Pattern2
{
    public static void main(String args[])
    {
	int alpha = 65;
	int r = 5;
        for (int m = 1; m <= r; m++)
        {
            for (int n = 1; n <= m; n++)
            {
                System.out.print((char)(alpha + n -1) + " ");
            }
            System.out.println();
        }
    }
}

Output

A
A B
A B C
A B C D
A B C D E

Pattern 3


class Pattern3
{
    public static void main(String args[])
    {
	int r = 5;
        for (int m = 1; m <= r; m++)
        {
            for (int n = 1; n <= m; n++)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output

*
* *
* * *
* * * *
* * * * *

Pattern 4


class Pattern4
{
    public static void main(String args[])
    {
	int r = 5;
        for (int m = 1; m <= r; m++)
        {
            for (int n = 1; n <= m; n++)
            {
                System.out.print(m + " ");
            }
            System.out.println();
        }
    }
}

Output

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

Pattern 5


class Pattern5
{
    public static void main(String args[])
    {
	int alpha = 65;
	int r = 5;
        for (int m = 1; m <= r; m++)
        {
            for (int n = 1; n <= m; n++)
            {
                System.out.print((char)(alpha + m -1) + " ");
            }
            System.out.println();
        }
    }
}

Output

A
B B
C C C
D D D D
E E E E E

Pattern 6


class Pattern6
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
		{ 
                        for (int n = r; n > m; n--)
			{
				System.out.print("  ");
			}
 
			for (int p = 1; p <= m; p++)
			{
				System.out.print("* ");
			}
			System.out.println();
		}
	}
}

Output

        *
      * *
    * * *
  * * * *
* * * * *

Pattern 7


class Pattern7
{
	public static void main(String args[])
	{
		int rows = 5;
		for (int m = 1; m <= rows; m++) 
                { 
                      for (int n = rows; n > m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= m; p++)
			{
				System.out.print(p + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 8


class Pattern8
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = 1; n < m; n++)
			{
				System.out.print(" ");
			}
			for (int p = m; p <= r; p++)
			{
				System.out.print(p + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 9


class Pattern9
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = 1; m <= r; m++) 
               { 
                        for (int n = r; n >= m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= m; p++)
			{
				System.out.print((char) (alpha + p - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

     A
    A B
   A B C
  A B C D
 A B C D E

Pattern 10


class Pattern10
{
	public static void main(String args[])
	{
		int alpha = 65;
		int rows = 5;
		for (int m = rows; m >= 1; m--)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print(" ");
			}
			for (int p = m; p <= 5; p++)
			{
				System.out.print((char) (alpha + p - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

     E
    D E
   C D E
  B C D E
 A B C D E

Pattern 11


class Pattern11
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= (m * 2) - 1; p++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

Output

    *
   ***
  *****
 *******
*********

Pattern 12


class Pattern12
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= m; p++)
			{
				System.out.print(m + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 13


class Pattern13
{
	public static void main(String[] args)
	{
		int alpha = 65;
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= m; p++)
			{
				System.out.print((char) (alpha + m - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

    A
   B B
  C C C
 D D D D
E E E E E

Pattern 14


class Pattern14
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print(n + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 15


class Pattern15
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
                {
                        for (int n = r; n >= m; n--)
			{
				System.out.print(n + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 16


class Pattern16
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = r; n >= m; n--)
			{
				System.out.print(n + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 17


class Pattern17
{
	public static void main(String args[])
	{
		int r = 5;
		int t = 1;
		for (int m = 1; m <= r; m++)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print(t + " ");
				t++;
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 18


class Pattern18
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print((char) (alpha + n - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

A B C D E
A B C D
A B C
A B
A

Pattern 19


class Pattern19
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = 1; m <= r; m++) 
               { 
                        for (int n = r; n >= m; n--)
			{
				System.out.print((char) (alpha + n - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

E D C B A
E D C B
E D C
E D
E

Pattern 20


class Pattern20
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = r; n >= m; n--)
			{
				System.out.print((char) (alpha + n - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

E
E D
E D C
E D C B
E D C B A

Pattern 21


class Pattern21
{
	public static void main(String args[])
	{
		int alpha = 65;
		int rows = 5;
		for (int m = 1; m <= rows; m++)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print((char) (alpha) + " ");
				alpha++;
			}
			System.out.println();
		}
	}
}

Output

A
B C
D E F
G H I J
K L M N O

Pattern 22


class Pattern22
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
		{ 
			for (int n = r; n > m; n--)
			{
				System.out.print(" ");
			}
			int temp = 1;
			for (int p = 1; p <= m; p++)
			{
				System.out.print(temp + " ");
				temp = temp * (m - p) / (p);
			}
			System.out.println();
		}
	}
}

Output

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Pattern 23


class Pattern23
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = 1; m <= r; m++) 
		{ 
			for (int n = r; n > m; n--)
			{
				System.out.print(" ");
			}
			int temp = 1;
			for (int p = 1; p <= m; p++)
			{
				System.out.print((char) (alpha - 1 + temp) + " ");
				temp = temp * (m - p) / (p);
			}
			System.out.println();
		}
	}
}

Output

    A
   A A
  A B A
 A C C A
A D F D A

Pattern 24


class Pattern24
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++)
		{
			for (int n = 1; n <= m; n++) 
			{ 
				System.out.print(n + " "); 
			} 
				System.out.println(); 
			}
                for (int m = r; m >= 1; m--)
		{
			for (int n = 1; n < m; n++)
			{
				System.out.print(n + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 25


class Pattern25
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = 1; m <= r; m++)
		{
			for (int n = 1; n <= m; n++) 
			{ 
				System.out.print((char) (alpha + n - 1) + " "); 
			} 
				System.out.println(); 
			} 
		for (int m = r; m >= 1; m--)
		{
			for (int n = 1; n <= m - 1; n++)
			{
				System.out.print((char) (alpha + n - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

A
A B
A B C
A B C D
A B C D E
A B C D
A B C
A B
A

Pattern 26


class Pattern26
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++)
		{
			for (int i = 1; i <= m; i++)
			{
				System.out.print("* ");
			}
			System.out.println();
		}
		for (int m = 1; m <= r - 1; m++) 
        {
            for (int i = r - 1; i >= m; i--)
			{
				System.out.print("* ");
			}
			System.out.println();
		}
	}
}

Output

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Pattern 27


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

Output

    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *

Pattern 28


class Pattern28
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print(n + " ");
			}
			System.out.println();
		}
		for (int m = 2; m <= r; m++)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print(n + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 29


class Pattern29
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = m; n >= 1; n--)
			{
				System.out.print(n + " ");
			}
			System.out.println();
		}
		for (int m = 2; m <= r; m++) 
                { 
                        for (int n = m; n >= 1; n--)
			{
				System.out.print(n + " ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 30


class Pattern30
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print((char) (alpha + n - 1) + " ");
			}
			System.out.println();
		}
		for (int m = 2; m <= r; m++)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print((char) (alpha + n - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

A B C D E
A B C D
A B C
A B
A
A B
A B C
A B C D
A B C D E

Pattern 31


public class Pattern31
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = m; n >= 1; n--)
			{
				System.out.print((char) (alpha + n - 1) + " ");
			}
			System.out.println();
		}
		for (int m = 2; m <= r; m++) 
                { 
                        for (int n = m; n >= 1; n--)
			{
				System.out.print((char) (alpha + n - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

E D C B A
D C B A
C B A
B A
A
B A
C B A
D C B A
E D C B A

Pattern 32


class Pattern32
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = r; m >= 1; m--)
		{
			for (int n = m; n >= 1; n--)
			{
				System.out.print("* ");
			}
			System.out.println();
		}
		for (int m = 2; m <= r; m++) 
                { 
                        for (int n = m; n >= 1; n--)
			{
				System.out.print("* ");
			}
			System.out.println();
		}
	}
}

Output

* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *

Pattern 33


class Pattern33
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print("  ");
			}
			for (int k = 1; k <= m; k++) 
                        { 
                                System.out.print(k + " "); 
                        } 
                        for (int l = m - 1; l >= 1; l--)
			{
				System.out.print(l + " ");
			}
			System.out.println();
		}
	}
}

Output

        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

Pattern 34


class Pattern34
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print("  ");
			}
			for (int k = 1; k <= m; k++) 
                        { 
                                System.out.print((char) (alpha + k - 1) + " "); 
                        } 
                        for (int l = m - 1; l >= 1; l--)
			{
				System.out.print((char) (alpha + l - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

        A
      A B A
    A B C B A
  A B C D C B A
A B C D E D C B A
 

Pattern 35


class Pattern35
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++)
		{
			for (int n = 1; n < m; n++)
			{
				System.out.print("0 ");
			}
			System.out.print(m + " ");
			for (int p = m; p < r; p++)
			{
				System.out.print("0 ");
			}
			System.out.println();
		}
	}
}

Output

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

Pattern 36


class Pattern36
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print(1 + " ");
			}
			for (int p = 1; p <= m; p++)
			{
				System.out.print(m + " ");
			}
			System.out.println();
		}
	}
}

Output

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
 

Pattern 37


class Pattern37
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print((char) (alpha) + " ");
			}
			for (int p = 1; p <= m; p++)
			{
				System.out.print((char) (alpha + m - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

A A A A A
A A A B B
A A C C C
A D D D D
E E E E E
 

Pattern 38


class Pattern38
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= m; p++)
			{
				System.out.print(p + " ");
			}
			System.out.println();
		}
		for (int m = 1; m <= r; m++)
		{
			for (int n = 1; n <= m; n++)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= r - m; p++)
			{
				System.out.print(p + " ");
			}
			System.out.println();
		}
	}
}

Output

    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
 

Pattern 39


class Pattern39
{
	public static void main(String args[])
	{
		int alpha = 65;
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n >= m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= m; p++)
			{
				System.out.print((char) (alpha + p - 1) + " ");
			}
			System.out.println();
		}
		for (int m = 1; m <= r; m++)
		{
			for (int n = 0; n <= m; n++)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= r - m; p++)
			{
				System.out.print((char) (alpha + p - 1) + " ");
			}
			System.out.println();
		}
	}
}

Output

     A
    A B
   A B C
  A B C D
 A B C D E
  A B C D
   A B C
    A B
     A
 

Pattern 40


class Pattern40
{
	public static void main(String args[])
	{
		int r = 5;
		for (int m = 1; m <= r; m++) 
                { 
                        for (int n = r; n > m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= (m * 2) - 1; p++) 
                        { 
                                System.out.print("*"); 
                        } 
                        System.out.println(); 
                } 
                for (int m = r - 1; m >= 1; m--)
		{
			for (int n = r - 1; n >= m; n--)
			{
				System.out.print(" ");
			}
			for (int p = 1; p <= (m * 2) - 1; p++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

Output

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
 

Basic Programs