File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments