print pattern in java 1 01 101 0101 10101

Solutions on MaxInterview for print pattern in java 1 01 101 0101 10101 by the best coders in the world

showing results for - "print pattern in java 1 01 101 0101 10101"
Aarón
15 Feb 2016
1// Print pattern in java 1 01 101 0101 10101
2import java.util.Scanner;
3public class PatternInJava
4{
5   public static void main(String[] args)
6   {
7      int a, b, x, y;
8      Scanner sc = new Scanner(System.in);
9      System.out.println("Please enter number of rows to print pattern: ");
10      int rows = sc.nextInt();
11      for(a = 1; a <= rows; a++)
12      {
13         if(a % 2 == 0)
14         {
15            x = 1;
16            y = 0;
17         }
18         else
19         {
20            x = 0;
21            y = 1;
22         }
23         for(b = 1; b <= a; b++)
24         {
25            if(b % 2 == 0)
26            {
27               System.out.print(x);
28            }
29            else
30            {
31               System.out.print(y);
32            }
33         }
34         System.out.println("");
35         sc.close();
36      }
37   }
38}