Pattern Printing

  1. Write a Javascript program using nested loops to print a square with 1's on the second diagonal and 0's on the rest of the square. Create a textbox for the user to enter the size of the square.
    Enter the size of the square: 10
    0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 1 0 0 0
    0 0 0 0 0 1 0 0 0 0
    0 0 0 0 1 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0
    0 1 0 0 0 0 0 0 0 0
    1 0 0 0 0 0 0 0 0 0
    
  2. Write a program using nested loop to print out a rectangle with 1's on the second diagonal and 0's on the rest of the squares.
    Enter the width of the rectangle: 19 
    Enter the height of the rectangle: 38
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //Press any key to continue . . .
    
    Enter the width of the rectangle: 38 
    Enter the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0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //Press any key to continue . . .
    
  3. Write a program to print 1's along a 45 degree line passing through a pre-specified point (x, y). Read this handouts.

    Enter the size of a square and the coordinates of a point:

    0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    Press any key to continue . . .