fork download
  1. class Ideone {
  2.  
  3. public static void printPattern(int n) {
  4. int totalLevels = n + 2;
  5. int patternWidth = 3 + 2 * n; // Total characters in each line (e.g., for n=3, 3 'g' + 3 'e' = 6, and 3 stars for the last line)
  6.  
  7. for (int i = 1; i <= totalLevels; i++) {
  8. if (i <= n) {
  9. // Levels 1 to n: "ggggge" pattern
  10. for (int j = 0; j < patternWidth; j++) {
  11. if (j < n + 1) { // 'g' characters
  12. System.out.print('g');
  13. } else { // 'e' characters
  14. System.out.print('e');
  15. }
  16. }
  17. } else if (i == n + 1) {
  18. // Level n+1: "g*ggge" pattern
  19. System.out.print('g');
  20. System.out.print('*');
  21. for (int j = 0; j < n; j++) {
  22. System.out.print('g');
  23. }
  24. for (int j = 0; j < n + 1; j++) {
  25. System.out.print('e');
  26. }
  27. } else {
  28. // Level n+2: "***eee" pattern
  29. for (int j = 0; j < n + 2; j++) {
  30. System.out.print('*');
  31. }
  32. for (int j = 0; j < n + 1; j++) {
  33. System.out.print('e');
  34. }
  35. }
  36. System.out.println(); // New line after each level
  37. }
  38. }
  39.  
  40. public static void main(String[] args) {
  41. int input = 3; // Example input
  42. printPattern(input);
  43. }
  44. }
Success #stdin #stdout 0.11s 54668KB
stdin
Standard input is empty
stdout
ggggeeeee
ggggeeeee
ggggeeeee
g*gggeeee
*****eeee