Java Pattern Programs

Here are some of the most asked and most important pattern programs in java programming language.

To make a pattern in any programming language you should have good command in logic of the basics of the language.

Pattern Program in Java

#1 Pattern program to print “Half Pyramid” in java.

Source Code:

public class patter {
	public static void main(String []s) {
		for(int i = 0; i <= 4; i++) {
			for(int j = 0; j <= 4; j++ ) {
				if(i == j || i > j) {
					System.out.print("*");
				}
			}
			System.out.print("\n");
		}
	}
}

Output:

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

#2 Pattern program to print “Inverted Half Pyramid” in java.

Source Code:

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

Output:

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

#3 Pattern program to print “Hollow Inverted Half Pyramid” in java.

Source Code:

public class patter {
	public static void main(String []s) {
		int k = 4;
		for(int i = 0; i <= 4; i++) {
			for(int j = k; j >= 0; j--) {
				if(i*j == 2 || i*j == 1) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
			}
			System.out.print("\n");
			k--;
		}
	}
}

Output:

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

#4 Write a java program to print below pattern?

*##########
**#########
***########
****#######
*****######
******#####
*******####
********###
*********##
**********#
***********

Source Code:

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter N between 0-999:");
        int N = sc.nextInt();
        int n = 0;
        if(N > 0 && N < 999){
            for(int i = 0; i<=N; i++){
                if(n<N){
                    for(int j = 0; j<=n; j++){
                        System.out.print("*");    
                    }
                }
                n++;
                for(int k = 0; k<N-n; k++){
                    System.out.print("#");    
                }
                System.out.println("");
            }    
        }
        else{
            System.out.println("Enter N between 0-999.");
        }
    }
}

Input & Output:

Enter N between 0-999:11
*##########
**#########
***########
****#######
*****######
******#####
*******####
********###
*********##
**********#
***********

#5 Pattern program to print “Solid Rectangle” in java.

Source Code:

public class patter {
	public static void main(String []s) {
		int r = 3;
		int c = 6;
		for(int i = 0; i<= r-1; i++) {
			for(int j = 0; j <= c-1; j++) {
				System.out.print(" *");
			}
			System.out.println();
		}
	}
}

Output:

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

#6 Pattern program to print “Hollow Rectangle” in java.

Source Code:

public class patter {
	public static void main(String []s) {
		int r = 3;
		int c = 6;
		for(int i = 0; i<= r-1; i++) {
			for(int j = 0; j <= c-1; j++) {
				if(i != 1) {
					System.out.print(" *");
				}
				else {
					if(j == 0 || j == 5) {
						System.out.print(" *");
					}
					else {
						System.out.print("  ");
					}
				}
			}
			System.out.println();
		}
	}
}

Output:

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