fork download
  1. class Ideone {
  2.  
  3. public static void generatePattern(int n) {
  4. int totalRows = n + 2;
  5. int totalChars = (n * 2) + 4; // Total characters in the 'g' rows
  6.  
  7. for (int i = 1; i <= totalRows; i++) {
  8. if (i <= n) {
  9. // Rows 1 to n: all 'g's followed by 'e's
  10. for (int j = 0; j < totalChars - 1; j++) {
  11. System.out.print("g");
  12. }
  13. System.out.println("e");
  14. } else if (i == n + 1) {
  15. // Row n+1: 'g's, a '*', and 'e's
  16. for (int j = 0; j < n - 1; j++) {
  17. System.out.print("g");
  18. }
  19. System.out.print("*");
  20. for (int j = 0; j < (totalChars - (n - 1) - 1 - 1); j++) {
  21. System.out.print("g");
  22. }
  23. System.out.println("e");
  24. } else { // i == n + 2
  25. // Row n+2: '*'s and 'e's
  26. for (int j = 0; j < n + 2; j++) {
  27. System.out.print("*");
  28. }
  29. for (int j = 0; j < totalChars - (n + 2); j++) {
  30. System.out.print("e");
  31. }
  32. System.out.println();
  33. }
  34. }
  35. }
  36.  
  37. public static void main(String[] args) {
  38.  
  39. generatePattern(5);
  40.  
  41. }
  42. }
Success #stdin #stdout 0.11s 52624KB
stdin
Standard input is empty
stdout
ggggggggggggge
ggggggggggggge
ggggggggggggge
ggggggggggggge
ggggggggggggge
gggg*gggggggge
*******eeeeeee