fork download
  1. import java.util.Scanner;
  2. class Heart_Shape {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. System.out.print("Enter the size of the heart (odd number): ");
  6. int size = scanner.nextInt();
  7.  
  8. if (size % 2 == 0) {
  9. System.out.println("Size must be an odd number.");
  10. return;
  11. }
  12.  
  13. for (int i = size / 2; i <= size; i += 2) {
  14. for (int j = 1; j < size - i; j += 2) {
  15. System.out.print(" ");
  16. }
  17.  
  18. for (int j = 1; j <= i; j++) {
  19. System.out.print("*");
  20. }
  21.  
  22. for (int j = 1; j <= size - i; j++) {
  23. System.out.print(" ");
  24. }
  25.  
  26. for (int j = 1; j <= i; j++) {
  27. System.out.print("*");
  28. }
  29.  
  30. System.out.println();
  31. }
  32.  
  33. for (int i = size; i >= 1; i--) {
  34. for (int j = i; j < size; j++) {
  35. System.out.print(" ");
  36. }
  37.  
  38. for (int j = 1; j <= (i * 2) - 1; j++) {
  39. System.out.print("*");
  40. }
  41.  
  42. System.out.println();
  43. }
  44.  
  45. scanner.close();
  46. }
  47. }
  48.  
Success #stdin #stdout 0.14s 56672KB
stdin
10
stdout
Enter the size of the heart (odd number): Size must be an odd number.