Pages

Friday, February 21, 2025

Star and Number Pattern Programming

1. Right-Angled Triangle Pattern with Star

java
Copy

public class Pattern {
    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("* ");
            }
            System.out.println();
        }
    }
}

Output:

Copy
* 
* * 
* * * 
* * * * 
* * * * * 

2. Right-Angled Triangle Pattern with Number Row wise 

java
Copy
public class Pattern {
    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(i);
            }
            System.out.println();
        }
    }
}

Output:

Copy
1
22
333
4444
55555 

3.  Right-Angled Triangle Pattern with Number Column wise 

java
Copy
public class Pattern {
    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(j);
            }
            System.out.println();
        }
    }
}

Output:

Copy
1
12
123
1234
12345 

4.  Square Pattern with Number Row wise 

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

Output:

Copy
11111
22222
33333
44444
55555 

5.  Square Pattern with Number Column wise 

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

Output:

Copy
12345
12345
12345
12345
12345

6. Inverted Right-Angled Triangle Pattern with Star

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

or

java
Copy
public class Pattern {
    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("* ");
            }
            System.out.println();
        }
    }
}

Output:

Copy
* * * * * 
* * * * 
* * * 
* * 
* 

7.  Inverted Right-Angled Triangle Pattern with Number Row wise 

java
Copy
public class Pattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = rows; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

Output:

Copy
55555
4444
333
22
1

8.  Inverted Right-Angled Triangle Pattern with Number Column wise 

java
Copy
public class Pattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = rows; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Output:

Copy
12345
1234
123
12
1 

9.  Right Sided Triangle Pattern with Decreasing Space & Increasing Star

java
Copy
public class Pattern {
    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("  ");
            }
                 for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
        
    System.out.println();

    }
}
}


or

ava
Copy
import java.util.*;

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

    }
}

Output:

Copy
          * 
        * * 
      * * * 
    * * * * 
  * * * * *

10. Pyramid / Hill Pattern

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

Output:

Copy
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

11. Number Pyramid Pattern

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

Output:

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


12. Diamond Pattern

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

Output:

Copy
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

13. Hollow Square Pattern

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

Output:

Copy
* * * * * 
*       * 
*       * 
*       * 
* * * * * 

14. Hollow Pyramid Pattern

java
Copy
public class Pattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j > i; j--) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                if (k == 1 || k == 2 * i - 1 || i == rows) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Output:

Copy
    *
   * *
  *   *
 *     *
*********

15. Floyd's Triangle Pattern

java
Copy
public class Pattern {
    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 + " ");
                num++;
            }
            System.out.println();
        }
    }
}

Output:

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

16.  Pascal's Triangle Pattern

java
Copy
public class Pattern {
    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 + " ");
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

Output:

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

17. Butterfly Pattern

java
Copy
public class Pattern {
    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("* ");
            }
            for (int j = 1; j <= 2 * (rows - i); j++) {
                System.out.print("  ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        for (int i = rows; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            for (int j = 1; j <= 2 * (rows - i); j++) {
                System.out.print("  ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

Copy
*         * 
* *     * * 
* * * * * * 
* *     * * 
*         * 

18. Zig-Zag Pattern

java
Copy
public class Pattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows; j++) {
                if (i == 1 || i == rows || j == rows - i + 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

Output:

Copy
* * * * * 
      *   
    *     
  *       
* * * * * 

19. Hollow Diamond Pattern

java
Copy
public class Pattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j > i; j--) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                if (k == 1 || k == 2 * i - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        for (int i = rows - 1; i >= 1; i--) {
            for (int j = rows; j > i; j--) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                if (k == 1 || k == 2 * i - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Output:

Copy
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *