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.

# Thread: How to print?

1. ## 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. ## 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. ## 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.

4. ## 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. ## Re: How to print?

Originally Posted by dicdic
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. ## 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. ## Re: How to print?

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

8. ## Re: How to print?

Originally Posted by vijay\$
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?