Print binary triangle Using Java
Previous | Home | Next |
Print binary triangle Using 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:");
String s1 = Row.readLine();
int n1 = Integer.parseInt(s1);
//code for Binary triangle
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:
10
1
01
101
0101
10101
010101
1010101
01010101
101010101
0101010101
Previous | Home | Next |