In this tutorial, you will learn to create left triangle pattern using binary number in java. The pattern wil consists of binary number with value 0 and 1. The first row will be print 0 and the next row will print 1. The full java source code is detailed below.
package patternchallenge; public class PatternChallenge { public static void main(String[] args) { PatternTriangle customTriangle2 = new PatternTriangle(); customTriangle2.leftTriangleCustom2(5); } } public class PatternTriangle { public void leftTriangleCustom2(int n){ for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ if(j%2==1){ System.out.print("1"); } else { System.out.print("0"); } } System.out.println(""); } } }
The output will be:
1 10 101 1010 10101