Header Ad

15+ pattern programs in Java programming

In this tutorial, we are going to write some pattern programs in java programming with practical program code and step-by-step full complete explanation.

15+ pattern programs in Java programming


First pattern program

 
class Pattern8
{
	public static void main(String args[])
	{
		int n, c, k, space, count = 1;
		space =n=5;
		for (c = 1; c <= n; c++)
		{
			for (k = 1; k < space; k++)
				System.out.print(" ");
			for (k = 1; k <= c; k++) {
				System.out.print("*");
			if (c > 1 && count < c)
			{
				System.out.print("A");
				count++;
			}
		}
			System.out.print("\n");
			space--;
			count = 1;
		}
	}
}


Output

 
     *
     *A*
    *A*A*
   *A*A*A*
  *A*A*A*A*


Second pattern program.

 
class Pattern9
{
	public static void main(String args[])
	{
		int rows, i, j, number= 1;
		rows=4;
		for(i=1; i <= rows; i++)
		{
			for(j=1; j <= i; ++j)
			{
				System.out.print(number);
				++number;
			}
			System.out.print("\n");
		}
	}
}


Output

 
1
23
456
78910


Third pattern program.

 
class Pattern10
{
	public static void main(String args[])
	{
		int rows, c = 1, space, i, j;
		rows=5;
		for(i=0; i<rows; i++)
		{
			for(space=1; space <= rows-i; space++)
			{
				System.out.print(" ");
			}
			for(j=0; j<=i; j++)
			{
				if (j==0 || i==0)
					c = 1;
				else
					c = c*(i-j+1)/j;
				System.out.print(" "+c);
			}
			System.out.print("\n");
		}
	}
}


Output

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


Fourth pattern program.

 
class Pattern11
{
	public static void main(String args[])
	{
		int i, space, rows, k=0, count = 0, count1 = 0;
		rows=5;
		for(i=1; i<=rows; ++i) 
		{
			for(space=1; space <= rows-i; ++space) 
			{
				System.out.print("  ");
				++count;
			}
			while(k != 2*i-1) 
			{
				if (count <= rows-1) 
				{
					System.out.print(" "+(i+k));
					++count;
				}
				else
				{
					++count1;
					System.out.print(" "+(i+k-2*count1));
				}
				++k;
			}
			count1 = count = k = 0;
			System.out.print("\n");
		}
	}
}


Output

 

           1
         2 3 2
       3 4 5 4 3
     4 5 6 7 6 5 4
   5 6 7 8 9 8 7 6 5


Fifth pattern program.

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


Output

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


Sixth pattern program.

 
class Pattern14
{
	public static void main(String args[]) 
	{
		int i,j,k,sp=1;
		for (i=5; i>=1; i--) 
		{
			for (k=sp; k>=0; k--) 
			{
				System.out.print(" ");// only 1 space
			}
			for (j=i; j>=1; j--) 
			{
				System.out.print("*");
			}
			sp = sp + 1;
			System.out.print("\n");
		}
	}
}


Output

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



Seventh pattern program.

 
class Pattern15
{
	public static void main(String args[]) 
	{
		int i,j,k,sp=1;
		for (i=1; i<=5; i++) 
		{
			for (k=sp; k<=5; k++) 
			{
				System.out.print(" ");
			}
			for (j=0; j< i; j++) 
			{
				System.out.print("*");
			}
			sp = sp + 1;
			System.out.print("\n");
		}
		sp = 1;
		for (i=4; i>=1; i--) 
		{
			for (k=sp; k>=0; k--) 
			{
				System.out.print(" ");
			}
			for (j=i; j>=1; j--) 
			{
				System.out.print("*");
			}
			sp = sp + 1;
			System.out.print("\n");
		}
	}
}


Output

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


Eight pattern program.

 
class Pattern16
{
	public static void main(String args[]) 
	{
		int i, j=5, k, x;
		for (i=1;i<=5;i++) 
		{
			for (k=1;k<=j;k++) 
			{
				System.out.print(" ");
			}
			for (x=1;x<=i;x++) 
			{
				System.out.print(i);
				System.out.print(" ");
			}
			System.out.print("\n");
			j=j-1;
		}
	}
}


Output

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


Ninth pattern program.

 
class Pattern17
{
	public static void main(String args[]) 
	{
		int i,j,k;
		for (i=1;i<=5;i++) 
		{
			for (j=5;j>=1;j--) 
			{
				if(j<=i)
					System.out.print(j);
				else
					System.out.print(" ");
			}
			System.out.print("\n");
		}
	}
}


Output

 
    1
   21
  321
 4321
54321


Ten pattern program.

 
class Pattern18
{
	public static void main(String args[]) 
	{
		int i,j,k;
		for (i=1;i<=5;i++) 
		{
			for (j=1;j<=5;j++) 
			{
				if(j<=i)
					System.out.print(j);
				else
					System.out.print(" ");
			}
			for (j=5;j>=1;j--) 
			{
				if(j<=i)
					System.out.print(j);
				else
					System.out.print(" ");
			}
			System.out.print("\n");
		}
	}
}


Output

1        1
12      21
123    321
1234  4321
1234554321


Eleven pattern program.

 
class Pattern19
{
	public static void main(String args[]) 
	{
		int i,j,k;
		for (i=1;i<=5;i++) 
		{
			j=i;
			for (k=1;k<=i;k++) 
			{
				System.out.print(j++);
			}
			System.out.print("\n");
		}
	}
}


Output

1
23
345
4567
56789

Twelve pattern program.

 
class Pattern20
{
	public static void main(String args[]) 
	{
		int i,j;
		for (i=1;i<=4;i++) 
		{
			for (j=i;j>1;j--)
			System.out.print(j);
				for (j=1;j<=i;j++)
					System.out.print(j);
			System.out.print("\n");
		}
	}
}


Output

 
1
212
32123
4321234


Thirteen pattern program.

 
class Pattern21
{
	public static void main(String args[]) 
	{
		int i,j;
		for (i=1;i<=5;i++) 
		{
			for (j=1;j<=5;j++) 
			{
				if(j==5 || j==1 || i==1 || i==5)
					System.out.print("1");
				else
					System.out.print(" ");
			}
			System.out.print("\n");
		}
	}
}


Output

 
11111
1   1
1   1
1   1
11111



Fourteen pattern program.

 
class Pattern22
{
	public static void main(String args[]) 
	{
		int i, j, k;
		for (i=1;i<=5;i++) 
		{
			for (j=i;j<5;j++) 
			{
				System.out.print(" ");
			}
			for (k=1;k<(i*2);k++) 
			{
				System.out.print(k);
			}
			System.out.print("\n");
		}
		for (i=4;i>=1;i--) 
		{
			for (j=5;j>i;j--) 
			{
				System.out.print(" ");
			}
			for (k=1;k<(i*2);k++)
			{
				System.out.print(k);
			}
			System.out.print("\n");
		}
	}	
}


Output

 
       1
      123
     12345
    1234567
   123456789
    1234567
     12345
      123
       1



Fifteen pattern program.

 
class Pattern23
{
	public static void main(String args[]) 
	{
		int i,j;
		for (i=1;i<=10;i++) 
		{
			for (j=1;j<=i;j++) 
			{
				System.out.print(i*j);
			}
			System.out.print("\n");
		}
	}
}


Output

 
1
24
369
481216
510152025
61218243036
7142128354249
816243240485664
91827364554637281
102030405060708090100


Sixteen pattern program.

 
class Pattern25
{
	public static void main(String args[]) 
	{
		int i, j, rows;
		/* Input number of rows from user */
		rows=8;
		for(i=1; i<=rows; i++) 
		{
			/* Print trailing spaces */
			for(j=1; j<=rows-i; j++) 
			{
				System.out.print(" ");
			}
			/* Print stars and center spaces */
			for(j=1; j<=rows; j++) 
			{
				if(i==1 || i==rows || j==1 || j==rows)
					System.out.print("*");
				else
					System.out.print(" ");
			}
			System.out.print("\n");
		}
	}
}


Output

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


Post a Comment

0 Comments