Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to print?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to print?

    Hi friend..

    How to print following pattern in java?

    11 12 13 14 15
    7 8 9 10
    4 5 6
    2 3
    1

    please solve this?


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: How to print?

    I think it would be better if you have a 2 dimensional array.
    1st element of array is array with single element: 1
    2nd element of array is array with 2 elements: 2, 3
    3rd element of array is array with 3 elements: 4, 5, 6
    4th element of array is array with 4 elements: 7, 8, 9, 10
    .
    .
    .
    .
    and so on

    the number of elements in 2nd dimension increases by 1 from first element (1st 2nd dimension)
    and you just need to have a counter to fill the 2nd dimension array

    [ [1], [2,3], [4,5,6].....]

    you just need a loop.

    and when you print it, make sure to start at the last element of the first dimension array..
    -sorry for my poor english

  3. #3
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Lightbulb Re: How to print?

    No Need for 2 dimensional array.

    To solve this problem, you have to see the logic of this problem. What does the problem show? At first, you have to print 11 (15-4) to 15, then 7 (10-3) to 10, then 4 (6-2) to 6, and so on.

    First, create a variable (n suppose) which stores 15, a temporary variable (e suppose) that stores 0 (for now).
    Now add two 'for' loops to your program (variables i,j), outer one will run from 0 to <5 and inner one will run from 0 to <5-i. Before j loop, initialize e with n-(5-(i+1)). Then in j loop print e+j. In the end, shift the control to next line by System.out.println() and also decrement n by (5-i).

    ...code removed

    Here, we are storing 15-4 in e first then printing 15-4 to 15, then decrementing n by 5 (so 15 becomes 10). Now again e stores 10-3, printing 10-3 to 10, n decremented by 4, n now stores 6, and so on. Does this help you.
    Last edited by copeg; June 9th, 2014 at 11:41 AM. Reason: Removed spoonfeeding

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: How to print?

    Yes there is no need to use 2 dimensional array for printing just

    11 12 13 14 15
    7 8 9 10
    4 5 6
    2 3
    1


    but what if the number of rows varies? Let say there is an input.
    Let say the users in put is 10. meaning were are going to have 10 rows:
    46 47 48 49 50 51 52 53 54 55
    37 38 39 40 41 42 43 44 45
    29 30 31 32 33 34 35 36
    22 23 24 25 26 27 28
    16 17 18 19 20 21
    11 12 13 14 15
    7 8 9 10
    4 5 6
    2 3
    1


    or the input is about 952? so there should be 952 rows.
    That is the reason why I suggested 2 dimensional array so that he/she can stores the generated sequence.
    Print just

    11 12 13 14 15
    7 8 9 10
    4 5 6
    2 3
    1

    is so easy, he/she could have done it hard coded. It will consist of 5 print statement (1 print statement will do also, provided a new line for String).

    That why I assumed that there is a possibility that the number of rows can vary.

  5. #5
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Lightbulb Re: How to print?

    Quote Originally Posted by dicdic View Post
    Yes there is no need to use 2 dimensional array for printing just

    11 12 13 14 15
    7 8 9 10
    4 5 6
    2 3
    1


    but what if the number of rows varies? Let say there is an input.
    Let say the users in put is 10. meaning were are going to have 10 rows:
    46 47 48 49 50 51 52 53 54 55
    37 38 39 40 41 42 43 44 45
    29 30 31 32 33 34 35 36
    22 23 24 25 26 27 28
    16 17 18 19 20 21
    11 12 13 14 15
    7 8 9 10
    4 5 6
    2 3
    1


    or the input is about 952? so there should be 952 rows.
    That is the reason why I suggested 2 dimensional array so that he/she can stores the generated sequence.
    Print just

    11 12 13 14 15
    7 8 9 10
    4 5 6
    2 3
    1

    is so easy, he/she could have done it hard coded. It will consist of 5 print statement (1 print statement will do also, provided a new line for String).

    That why I assumed that there is a possibility that the number of rows can vary.

    I know that, but the above program was just for 5 lines. For user input of lines, let us take the input and store it in variable m. Now, by your method, if user will input 952 or 2000, your program will have to make a 2D array of size 952*952=906304 or 2000*2000=4,000,000 i.e. a array of size 4 million!
    My method is much simpler and will not require so much of space from the RAM. For m no. of lines, you just have to add this:-

     for(int i=0; i<m; i++)
     {for(int j=0; j<=i; j++)
      {n=r;
       r++;
      }
     }

    where n stores anything initially (let it store 0) and r stores 1 initially (int r=1; ).

    then replace 5 by m in the previous code given by me, i.e.

    ...

    now it should satisfy you. Satisfied!

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to print?

    Come on folks, lets refrain from spoonfeeding. One or more posts above have been edited as a result

    @vijay$, what have you tried?

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to print?

    hi..i cant solve this problem..if i get code i feel good.

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to print?

    Quote Originally Posted by vijay$ View Post
    i cant solve this problem.
    Sure you can...but we can only guess where you are stuck. Break the problem down and describe where you are stuck. Do you know how to write a loop? Is it the number sequence that is the problem?

Similar Threads

  1. print A-Z without O I J
    By Billzhang in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 15th, 2013, 06:57 AM
  2. CANT PRINT
    By yahel_s7 in forum Java IDEs
    Replies: 5
    Last Post: November 1st, 2013, 06:16 AM
  3. CANT PRINT
    By yahel_s7 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2013, 12:59 PM
  4. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  5. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM