Advertisement
Shailrshah

Alternate 1s' and 0s' Pattern

Sep 28th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Input{
  3.     int getInput(String prompt){
  4.         System.out.print(prompt);
  5.         Scanner sc = new Scanner(System.in);
  6.         if(!sc.hasNextInt())
  7.             System.out.println("Enter a whole number.");
  8.         else return sc.nextInt();
  9.         return 0;
  10.     }
  11. }
  12. class PatternOne{
  13.     public static void main(String args[]){
  14.         int n = new Input().getInput("Enter the number of rows:  ");
  15.         for(int i = 0; i < n; i++){
  16.             for(int j = 0; j <= i; j++){
  17.                 if((i+j+1)%2 == 0) System.out.print(0);
  18.                 else System.out.print(1);
  19.             }
  20.             System.out.println();
  21.         }
  22.     }
  23. }
  24. /*Output:-
  25. Enter the number of rows:  5
  26. 1
  27. 01
  28. 101
  29. 0101
  30. 10101 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement