Exercise PrintPatterns ~ Programming is Fun
 Programming is Fun!

www.anandsingh.net
Subscribe For Free Updates!

We'll not spam mate! We promise.

Saturday, September 6, 2014

Exercise PrintPatterns

Write a method to print each of the followings patterns using nested loops in a class called PrintPatterns. The signatures of the methods are:
public static void printPatternX(int size)  // 'X' from 'A' to ..., size is a positive integer.
#                    # # # # # # # #      # # # # # # # #                    #
# #                  # # # # # # #          # # # # # # #                  # #
# # #                # # # # # #              # # # # # #                # # #
# # # #              # # # # #                  # # # # #              # # # #
# # # # #            # # # #                      # # # #            # # # # #
# # # # # #          # # #                          # # #          # # # # # #
# # # # # # #        # #                              # #        # # # # # # #
# # # # # # # #      #                                  #      # # # # # # # #
     (a)                   (b)                  (c)                  (d)
   
Hints: On the diagonal, row = col.  On the opposite diagonal, row + col = size + 1.
   
# # # # # # #    # # # # # # #    # # # # # # #    # # # # # # #    # # # # # # #
#           #      #                        #        #       #      # #       # #
#           #        #                    #            #   #        #   #   #   #
#           #          #                #                #          #     #     #
#           #            #            #                #   #        #   #   #   #
#           #              #        #                #       #      # #       # #
# # # # # # #    # # # # # # #    # # # # # # #    # # # # # # #    # # # # # # #
     (e)               (f)             (g)              (h)              (i)
   
# # # # # # # # # # #                #                          #
  # # # # # # # # #                # # #                      # # #
    # # # # # # #                # # # # #                  # # # # #
      # # # # #                # # # # # # #              # # # # # # #
        # # #                # # # # # # # # #          # # # # # # # # #
          #                # # # # # # # # # # #      # # # # # # # # # # #
         (j)                        (k)                 # # # # # # # # #
                                                          # # # # # # #
                                                            # # # # #
                                                              # # #
                                                                #
                                                               (l)
   
1                    1 2 3 4 5 6 7 8                    1      8 7 6 5 4 3 2 1
1 2                    1 2 3 4 5 6 7                  2 1      7 6 5 4 3 2 1
1 2 3                    1 2 3 4 5 6                3 2 1      6 5 4 3 2 1
1 2 3 4                    1 2 3 4 5              4 3 2 1      5 4 3 2 1
1 2 3 4 5                    1 2 3 4            5 4 3 2 1      4 3 2 1
1 2 3 4 5 6                    1 2 3          6 5 4 3 2 1      3 2 1
1 2 3 4 5 6 7                    1 2        7 6 5 4 3 2 1      2 1
1 2 3 4 5 6 7 8                    1      8 7 6 5 4 3 2 1      1
     (m)                  (n)                    (o)                (p)
   
              1                    1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
            1 2 1                    1 2 3 4 5 6 7 6 5 4 3 2 1
          1 2 3 2 1                    1 2 3 4 5 6 5 4 3 2 1
        1 2 3 4 3 2 1                    1 2 3 4 5 4 3 2 1
      1 2 3 4 5 4 3 2 1                    1 2 3 4 3 2 1
    1 2 3 4 5 6 5 4 3 2 1                    1 2 3 2 1
  1 2 3 4 5 6 7 6 5 4 3 2 1                    1 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1                    1
             (q)                                (r)
   
1                           1      1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2                       2 1      1 2 3 4 5 6 7   7 6 5 4 3 2 1
1 2 3                   3 2 1      1 2 3 4 5 6       6 5 4 3 2 1
1 2 3 4               4 3 2 1      1 2 3 4 5           5 4 3 2 1
1 2 3 4 5           5 4 3 2 1      1 2 3 4               4 3 2 1
1 2 3 4 5 6       6 5 4 3 2 1      1 2 3                   3 2 1
1 2 3 4 5 6 7   7 6 5 4 3 2 1      1 2                       2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1      1                           1
             (s)                                (t)
   
              1
            2 3 2
          3 4 5 4 3
        4 5 6 7 6 5 4
      5 6 7 8 9 8 7 6 5
    6 7 8 9 0 1 0 9 8 7 6
  7 8 9 0 1 2 3 2 1 0 9 8 7 
8 9 0 1 2 3 4 5 4 3 2 1 0 9 8
             (u)
Comment your Answers :)


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

1 comments:

  1. can i get the code of the letter m and letter p sir

    ReplyDelete