Binary Triangle

Binary Triangle

Previous Home Next

 

Program for produce the Binary Triangle.

 

 

/*
 * Save as a BinaryTriangle.java
 * Program for Binary triangle
 */
package r4r.co.in;

import java.io.*;

public class BinaryTriangle {

    public static void main(String[] args) throws IOException {
        BufferedReader Row = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the row of the Triangle:");

        //Read the line from the console
        String s1 = Row.readLine();
        //type conversion (String to Integer)
        int n1 = Integer.parseInt(s1);

        //code for Binary triangle

        System.out.println("\n Here the triange:");
        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= i; j++) {
                if (i % 2 == 0) {
                    if (j % 2 == 0) {
                        System.out.print("1");
                    } else {
                        System.out.print("0");
                    }
                } else {
                    if (j % 2 == 0) {
                        System.out.print("0");
                    } else {
                        System.out.print("1");
                    }
                }
            }
            System.out.print("\n");
        }
    }
}

Enter the row of the Triangle:
10

 Here the triange:
1
01
101
0101
10101
010101
1010101
01010101
101010101
0101010101

Previous Home Next