-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP23_BinaryNumberTriangle.java
More file actions
38 lines (34 loc) · 1.01 KB
/
Copy pathP23_BinaryNumberTriangle.java
File metadata and controls
38 lines (34 loc) · 1.01 KB
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
package programs;
/**
* ============================================================
* PROGRAM 23: Alternating 0-1 Binary Triangle
* ============================================================
* Problem: WAP to print a binary 0-1 alternating triangle pattern.
*
* Example (n = 5):
* 1
* 0 1
* 1 0 1
* 0 1 0 1
* 1 0 1 0 1
* ============================================================
*/
public class P23_BinaryNumberTriangle {
public static void printBinaryTriangle(int n) {
System.out.println("=== 0-1 BINARY TRIANGLE (n=" + n + ") ===");
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= r; c++) {
// If (r + c) is even -> print 1, else print 0
if ((r + c) % 2 == 0) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
printBinaryTriangle(5);
}
}