program to display triangle binary pattern

Here, we will discuss Java program to display triangle binary pattern. we use binary Floyd’s triangle program with coding.

  • We get input from user using Scanner class in Java.
  • Create nested for loop and it will be execute number of row times.
  • Inside if condition j%2==0 means then print zero else they print 1.
				
					import java.util.Scanner;

public class BinaryPattern {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("enter the number of rows: ");
		int row = sc.nextInt();
		
		for (int i = 1; i <= row; i++) {
			for (int j = 1; j <= i; j++) {
				if (j % 2 == 0)
					System.out.print("0");
				else
					System.out.print("1");

			}
			System.out.println();
		}
	}
}
				
			

Output :-
enter the number of rows: 7
1
10
101
1010
10101
101010
1010101