# help in writing programme

DharmaRao Edula
Greenhorn
Posts: 10
Hi

I want to print numbers using loops in the below format

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

can some body help me in writing the logic.

Sagar Rohankar
Ranch Hand
Posts: 2907
1
Show your effort first . Where you get stuck ? Post your code which you wrote up till now ?

DharmaRao Edula
Greenhorn
Posts: 10
i wrote like this

Class Test {

public static void main ( String[] args)
{
int j = 1
for(int i =1;i<5;i++)
{
int k=1;
while(K<i)
{
S.o.p(j);
}

}

Sagar Rohankar
Ranch Hand
Posts: 2907
1
Whenever i come across such problem i construct it like i am solving any matrix problem,,

Like here , you can find 5 rows and 5 column ..

Now you can apply two loop, one for row and another for column index.

Something like this :

Note : pl use code tag to surround you code snippet !!

DharmaRao Edula
Greenhorn
Posts: 10
Thanks for the suggestion. I tried it but i am not getting the required out put. could you please place the working code.

Sagar Rohankar
Ranch Hand
Posts: 2907
1
I ll give you a working code , on 1 condition only ,if you try to solve this on your own .

Use the logic of two loop which i provided you earlier !!, Best Luck

Seamus Minogue
Ranch Hand
Posts: 41
I'm not really a fan of the matrix solution...

You either need to problematically populate the matrix up front... in which case why not just print off the values then?

OR

You have hard coded in the values. Which feels almost like cheating to me...

True it is a quick and perfectly workable solution to this but this feels more like a "how to think about solving a problem" question...

I like the:

example Sagar gave to illustrate one possible solution.

The key to this, DharmaRao is the numbers.

Specifically the number of chars printed is the same as the line number.

Meaning:
line 1: 1 *
line 2: 2 *'s
line 3: 3 *'s

and so on.

All you need to do is think of how to use numbers instead of *'s

I really hope I haven't given too much away with this answer as Sagar is perfectly correct in that you need to find the solution for yourself.

Best of Luck!

Josh Burgans
Greenhorn
Posts: 1
[ UD: Like Jesper says below, LetThemDoTheirOwnHomework ]
[ July 02, 2008: Message edited by: Ulf Dittmer ]

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Josh, welcome to JavaRanch.

Please let them do their own homework - by doing other people's homework you are not helping them to learn Java.

Seamus Minogue
Ranch Hand
Posts: 41
Apologies...

Shaan Shar
Ranch Hand
Posts: 1249
Originally posted by Seamus Minogue:
Apologies...

Why you are saying this???

were you "Josh Burgans"

Seamus Minogue
Ranch Hand
Posts: 41
No but it seemed like I made the same mistake by not just providing the answer.

*shrug*