Skip to content

Commit 3cee0f4

Browse files
Merge pull request #167 from Tdms2589/main
hacktoberfest-2022
2 parents 8912279 + 9dd5335 commit 3cee0f4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Sierpinski.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Java program to print
2+
// sierpinski triangle.
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Sierpinski
7+
{
8+
static void printSierpinski(int n)
9+
{
10+
for (int b = n - 1; b >= 0; b--) {
11+
12+
// printing space till
13+
// the value of y
14+
for (int i = 0; i < b; i++) {
15+
System.out.print(" ");
16+
}
17+
18+
// printing '*'
19+
for (int a = 0; a + b < n; a++) {
20+
21+
// printing '*' at the appropriate
22+
// position is done by the and
23+
// value of x and y wherever value
24+
// is 0 we have printed '*'
25+
if ((a & b) != 0)
26+
System.out.print(" "
27+
+ " ");
28+
else
29+
System.out.print("* ");
30+
}
31+
32+
System.out.print("\n");
33+
}
34+
}
35+
36+
// Driver code
37+
public static void main(String args[])
38+
{
39+
int n = 16;
40+
41+
// Function calling
42+
printSierpinski(n);
43+
}
44+
}
45+

0 commit comments

Comments
 (0)