Answers for "Print pattern in java 1 01 101 0101 10101"

1

Print pattern in java 1 01 101 0101 10101

// Print pattern in java 1 01 101 0101 10101
import java.util.Scanner;
public class PatternInJava
{
   public static void main(String[] args)
   {
      int a, b, x, y;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of rows to print pattern: ");
      int rows = sc.nextInt();
      for(a = 1; a <= rows; a++)
      {
         if(a % 2 == 0)
         {
            x = 1;
            y = 0;
         }
         else
         {
            x = 0;
            y = 1;
         }
         for(b = 1; b <= a; b++)
         {
            if(b % 2 == 0)
            {
               System.out.print(x);
            }
            else
            {
               System.out.print(y);
            }
         }
         System.out.println("");
         sc.close();
      }
   }
}
Posted by: Guest on February-18-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language