Java Programs for patterns – For Freshers

				
					Write a Java program to print a pyramid pattern.

public class PyramidPattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}

Output:
    *
   ***
  *****
 *******
*********


				
			
				
					Write a Java program to print a right-angled triangle pattern.

public class RightAngleTriangle {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}
Output:
*
**
***
****
*****

				
			
				
					Write a Java program to print an inverted pyramid pattern.

public class InvertedPyramid {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = rows; i >= 1; i--) {
            for (int j = rows; j > i; j--) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}
Output:
*********
 *******
  *****
   ***
    *

				
			
				
					Write a Java program to print a number pyramid pattern.

public class NumberPyramid {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");  // Print numbers
            }
            System.out.println();  // Move to the next line
        }
    }
}

Output:
    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 

				
			
				
					Write a Java program to print a hollow rectangle pattern.

public class HollowRectangle {
    public static void main(String[] args) {
        int rows = 5, columns = 7;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= columns; j++) {
                if (i == 1 || i == rows || j == 1 || j == columns) {
                    System.out.print("*");  // Print stars at the borders
                } else {
                    System.out.print(" ");  // Print spaces inside
                }
            }
            System.out.println();  // Move to the next line
        }
    }
}
Output:
*******
*     *
*     *
*     *
*******

				
			
				
					Write a Java program to print Floyd's Triangle, which prints numbers in a triangular form.

public class FloydsTriangle {
    public static void main(String[] args) {
        int rows = 5;
        int num = 1;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(num + " ");  // Print numbers
                num++;
            }
            System.out.println();  // Move to the next line
        }
    }
}

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


				
			
				
					Write a Java program to print a square pattern using numbers.

public class NumberSquare {
    public static void main(String[] args) {
        int size = 5;
        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size; j++) {
                System.out.print(i + " ");  // Print the row number
            }
            System.out.println();  // Move to the next line
        }
    }
}
Output:
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 
5 5 5 5 5


				
			
				
					Write a Java program to print Pascal's triangle.

public class PascalsTriangle {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 0; i < rows; i++) {
            int number = 1;
            for (int j = 0; j <= i; j++) {
                System.out.print(number + " ");  // Print the number
                number = number * (i - j) / (j + 1);  // Calculate the next number in the row
            }
            System.out.println();  // Move to the next line
        }
    }
}
Output:
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1

				
			
				
					Write a Java program to print an hourglass pattern.

public class HourglassPattern {
    public static void main(String[] args) {
        int rows = 5;
        
        // Upper half of the hourglass
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 0; j < (2 * (rows - i) - 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
        
        // Lower half of the hourglass
        for (int i = 1; i < rows; i++) {
            for (int j = rows - 1; j > i; j--) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 0; j < (2 * i + 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}

Output:
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********

				
			
				
					Write a Java program to print a pyramid pattern.

public class PyramidPattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}

				
			
				
					Write a Java program to print a pyramid pattern.

public class PyramidPattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}

				
			
				
					Write a Java program to print a pyramid pattern.

public class PyramidPattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}

				
			
				
					Write a Java program to print a pyramid pattern.

public class PyramidPattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}

				
			
				
					Write a Java program to print a pyramid pattern.

public class PyramidPattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");  // Print spaces
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");  // Print stars
            }
            System.out.println();  // Move to the next line
        }
    }
}

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


				
			

Leave a Comment

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

Scroll to Top