forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSierpinski.java
More file actions
45 lines (37 loc) · 767 Bytes
/
Sierpinski.java
File metadata and controls
45 lines (37 loc) · 767 Bytes
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
39
40
41
42
43
44
// Java program to print
// sierpinski triangle.
import java.util.*;
import java.io.*;
class Sierpinski
{
static void printSierpinski(int n)
{
for (int b = n - 1; b >= 0; b--) {
// printing space till
// the value of y
for (int i = 0; i < b; i++) {
System.out.print(" ");
}
// printing '*'
for (int a = 0; a + b < n; a++) {
// printing '*' at the appropriate
// position is done by the and
// value of x and y wherever value
// is 0 we have printed '*'
if ((a & b) != 0)
System.out.print(" "
+ " ");
else
System.out.print("* ");
}
System.out.print("\n");
}
}
// Driver code
public static void main(String args[])
{
int n = 16;
// Function calling
printSierpinski(n);
}
}