Java Pattern Programs of Stars, Numbers and Alphabets

Here you will get list of java programs to print patterns of stars, numbers and alphabets. If you want code for any particular pattern then mention it in comment section, I will try to add the program here.

Java Pattern Programs

Patterns of Stars

Pattern 1:

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

Pattern 2:

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

			for(int k = 1; k <= i; ++k)
				System.out.print("*");
			
			System.out.print("\n");
		}
	}
}

Pattern 3:

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

Pattern 4:

*****
 ****
  ***
   **
    *
public class PrintPattern {
	public static void main(String args[]) {
		for(int i = 1; i <= 5; ++i) {
			for(int j = 1; j < i; ++j)
				System.out.print(" ");

			for(int k = 5; k >= i; --k)
				System.out.print("*");
			
			System.out.print("\n");
		}
	}
}

Pattern 5:

    *
   ***
  *****
 *******
*********
public class PrintPattern {
	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*2); ++k)
				System.out.print("*");
				
			System.out.print("\n");
		}
	}
}

Pattern 6:

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

Pattern 7:

    *
   * *
  *   *
 *     *
*********
public class PrintPattern {
	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*2); ++k)
				if(k==1 || k==(i*2)-1 || i==5)
					System.out.print("*");
				else
					System.out.print(" ");
				
			System.out.print("\n");
		}
	}
}

Pattern 8:

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

Pattern 9:

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

Pattern 10:

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

Patterns of Numbers

Pattern 1:

1
12
123
1234
12345
public class PrintPattern {
	public static void main(String args[]) {
		for(int i = 1; i <= 5; ++i) {
			for(int j = 1; j <= i; ++j)
				System.out.print(j);
			
			System.out.print("\n");
		}
	}
}

Pattern 2:

1
22
333
4444
55555
public class PrintPattern {
	public static void main(String args[]) {
		for(int i = 1; i <= 5; ++i) {
			for(int j = 1; j <= i; ++j)
				System.out.print(i);
			
			System.out.print("\n");
		}
	}
}

Pattern 3:

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

Pattern 4:

This pattern is also known as pascal’s triangle.

    1 
   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1
public class PrintPattern {
	public static void main(String args[]) {
		int i,j,k,n=5;
		
		for(i=0;i<n;++i){
	        //loop to print spaces at starting of each row
	        for(j=1;j<=(n-i-1);++j){
	            System.out.print(" ");
	         }
	        
	        //loop to calculate each value in a row and print it
	        for(k=0;k<=i;++k){
	            System.out.print(fact(i)/(fact(i-k)*fact(k)) + " ");
	        }
	        
	        System.out.print("\n");
	    }
	}
	
	//function to calculate factorial
	static long fact(int x){
		int i;
		long f=1;
		
		for(i=1;i<=x;++i){
			f=f*i;
		}
		
		return f;
	}
}

Patterns of Alphabets

Pattern 1:

A
AB
ABC
ABCD
ABCDE
public class PrintPattern {
	public static void main(String args[]) {
		for(int i = 1; i <= 5; ++i) {
			for(int j = 0; j < i; ++j)
				System.out.print((char)('A' + j));
			
			System.out.print("\n");
		}
	}
}

Pattern 2:

A
BB
CCC
DDDD
EEEEE
public class PrintPattern {
	public static void main(String args[]) {
		for(int i = 0; i < 5; ++i) {
			for(int j = 0; j <= i; ++j)
				System.out.print((char)('A' + i));
			
			System.out.print("\n");
		}
	}
}

Pattern 3:

A
BA
CBA
DCBA
EDCBA
public class PrintPattern {
	public static void main(String args[]) {
		for(int i = 0; i < 5; ++i) {
			for(int j = 0, k=i; j <= i; ++j, --k)
				System.out.print((char)('A' + k));
			
			System.out.print("\n");
		}
	}
}

Feel free to ask your queries related to above java pattern programs in comment section.

20 thoughts on “Java Pattern Programs of Stars, Numbers and Alphabets”

    1. shivani madhavaram

      class Untitled
      {
      public static void main(String[ ] args)
      {
      for(int i=1;i<=3;i++)
      {
      for(int j=1;j<=1;j++)
      {
      if(i==1)
      {
      System.out.print("a b");
      }
      else if(i==2)
      {
      System.out.print("aa bb");
      }
      else
      {
      System.out.print("aaabbb");
      }
      }
      System.out.println( );
      }
      }
      }

    1. shivani madhavaram

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

    1. Pravleen Singh

      int max=10;
      int counter=0;
      int k=1;
      for(int i=0;i<max;i++)
      {

      for(int j=0;j<=i;j++)
      {
      if(j%2==0)
      {
      System.out.print("a");
      }
      else
      {
      System.out.print(k);
      k+=1;
      }
      }
      System.out.println();
      }

    1. How to do this program?
      Thank you
      *** *** ** *** **
      ** ** ** ** ** ** ** **
      ** *** ** ** ** ** **
      ** * ** ** ** ** **
      ** ** ** ** ** **
      ** ** **** ****

Leave a Comment

Your email address will not be published. Required fields are marked *