To print a binary pattern in java is simple thing. its same like printing stars the only logic changes is here we have to print 1 or 0 instead of star. and thats also very easy. if we observe when (i+j%2==0) that time we have to print 1 else 0.
public class pattern2 { public static void main(String[] args) { for(int i=0;i<=4;i++) { for(int j=0;j<=i;j++) { if((i+j)%2==0) { System.out.print(1); } else { System.out.print(0); } } System.out.println(); } } }Output:-
1
01
101
0101
10101
No comments:
Post a Comment