Advertisement
Guest User

Java Triangle For Loop

a guest
Nov 18th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. // Java code to demonstrate star pattern
  4. public class GeeksForGeeks
  5. {
  6. // Function to demonstrate printing pattern
  7. public static void printTriagle(int n)
  8. {
  9. // outer loop to handle number of rows
  10. // n in this case
  11. for (int i=0; i<n; i++)
  12. {
  13.  
  14. // inner loop to handle number spaces
  15. // values changing acc. to requirement
  16. for (int j=n-i; j>1; j--)
  17. {
  18. // printing spaces
  19. System.out.print(" ");
  20. }
  21.  
  22. // inner loop to handle number of columns
  23. // values changing acc. to outer loop
  24. for (int j=0; j<=i; j++ )
  25. {
  26. // printing stars
  27. System.out.print("* ");
  28. }
  29.  
  30. // ending line after each row
  31. System.out.println();
  32. }
  33. }
  34.  
  35. // Driver Function
  36. public static void main(String args[])
  37. {
  38. int n = 5;
  39. printTriagle(n);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement