Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public static void printStars(int number) {
  2. for (int i = 0; i < number; i++) {
  3. System.out.print("*");
  4. }
  5. System.out.println(" ");
  6.  
  7. }
  8.  
  9. public static void printSpaces(int number) {
  10. for (int i = 0; i < number; i++) {
  11. System.out.print(" ");
  12. }
  13. }
  14.  
  15. public static void printTriangle(int number) {
  16. // Each row of Triangles are made up of stars and spaces
  17. // may be I should count their numbers and come up with general
  18. // formula that satisfies the condition at each row.
  19. for (int i = 0; i < number; i++) {
  20.  
  21. // how many spaces do I need at each line?
  22. // for the first row I would need 4 spaces
  23. // for the second row I would need 3 spaces
  24. // for the third row I would need 2 spaces
  25. // for the fourth row I would need 1 space.
  26. // for the fifth row I would need 0 spaces
  27.  
  28. printSpaces(number - i - 1);
  29. // how many starts do I need at each line?
  30.  
  31. // for the first row I would need 1 star
  32. // for the second row I would need 2 stars
  33. // for the third row I would need 3 stars
  34. // for the fourth row I would need 4 stars
  35. // for the fifth row I would need 5 stars.
  36.  
  37. printStars(i + 1);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement