Created
February 4, 2015 14:35
-
-
Save adityasatrio/a972f63e3a9b13a4dd87 to your computer and use it in GitHub Desktop.
equilateral triangular using loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class Triangular { | |
public static int triangular(int totalDot) { | |
System.out.print("\n"); | |
for (int i = 0; i < totalDot; i++) { | |
for (int j = 1; j <= totalDot - i; j++) { | |
System.out.print("*"); | |
} | |
System.out.println(); | |
} | |
System.out.print("\n"); | |
} | |
public static void main(String[] args) { | |
Loop.triangular(1); | |
Loop.triangular(2); | |
Loop.triangular(3); | |
Loop.triangular(4); | |
} | |
} | |
/** | |
Triangular numbers are so called because of the equilateral triangular shape that they occupy when laid out as dots. e.g. | |
triangle(1) | |
* | |
=> 1 | |
triangle(2) | |
** | |
* | |
=> 3 | |
triangle(3) | |
*** | |
** | |
* | |
=> 6 | |
Write a function `triangle` that prints the triangle and returns the triangular number -- number of *'s printed. It should return 0 for out of range values, but you will always be passed an integer. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
equilateral triangular using loop: i spent 98.58 minute for pieces of codes, shame on me, - mediocre programmer