fork download
  1. import java.util.Scanner;
  2.  
  3. class Ideone {
  4.  
  5. public static void main(String[] args) {
  6. // Scanner scanner = new Scanner(System.in);
  7. // System.out.print("Enter an integer (e.g., 3 or 5): ");
  8. // int n = scanner.nextInt();
  9. // printPattern(n);
  10. // scanner.close();
  11.  
  12. printPattern(3);
  13. }
  14.  
  15. public static void printPattern(int n) {
  16. // Calculate the height of the star pyramid
  17. int starPyramidHeight = (n / 2) + 1;
  18.  
  19. // Top rectangular part of the pattern
  20. for (int i = 0; i < n - starPyramidHeight; i++) {
  21. printSpaces(2 * n);
  22. printEs(n);
  23. System.out.println();
  24. }
  25.  
  26. // Middle section with star pyramid
  27. for (int i = 0; i < starPyramidHeight; i++) {
  28. // Print spaces for the left side of the pattern
  29. int numSpacesLeft = 2 * n - (starPyramidHeight - 1 - i);
  30. printSpaces(numSpacesLeft);
  31.  
  32. // Print the pyramid stars
  33. int numStars = 2 * i + 1;
  34. printStars(numStars);
  35.  
  36. // Print 'e' characters on the right
  37. int numEs = n - i;
  38. printEs(numEs);
  39.  
  40. System.out.println();
  41. }
  42.  
  43. // Bottom section with full row of stars
  44. printStars(2 * n);
  45. printEs(n);
  46. System.out.println();
  47. }
  48.  
  49. private static void printSpaces(int count) {
  50. for (int i = 0; i < count; i++) {
  51. System.out.print(' ');
  52. }
  53. }
  54.  
  55. private static void printEs(int count) {
  56. for (int i = 0; i < count; i++) {
  57. System.out.print('e');
  58. }
  59. }
  60.  
  61. private static void printStars(int count) {
  62. for (int i = 0; i < count; i++) {
  63. System.out.print('*');
  64. }
  65. }
  66. }
Success #stdin #stdout 0.1s 52724KB
stdin
Standard input is empty
stdout
      eee
     *eee
      ***ee
******eee