-
Notifications
You must be signed in to change notification settings - Fork 1
/
Triangle (2).java
38 lines (30 loc) · 940 Bytes
/
Triangle (2).java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Write a program Triangle.java that takes a command-line argument N and prints an N-by-N triangular pattern like the one below
. . . . . .
* . . . . .
* * . . . .
* * * . . .
* * * * . .
* * * * * .
*/
public class Triangle
{
public static void main(String[] args)
{
/*
Upon analysing the diagram, it can be observed that horizontally, dot (.) is printed N minus M times,
WHEREAS,
Star (*) is printed M times
N is the number entered by the user and M is the number of row starting from zero.
*/
int num = Integer.parseInt(args[0]); // num = N
// a = M
for (int a = 0; a < num; a++) // Loop is run 'num' times
{
// The following two loops operate the printing of dot (.) and star (.) on a single line
for (int b = 0; b < a; b++) System.out.print("* ");
for (int b = 0; b < num-a; b++) System.out.print(". ");
System.out.println();
}
}
}