*
The moose likes Java in General and the fly likes help in writing programme Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "help in writing programme" Watch "help in writing programme" New topic
Author

help in writing programme

DharmaRao Edula
Greenhorn

Joined: Feb 28, 2008
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.

thanks in adavance
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Show your effort first . Where you get stuck ? Post your code which you wrote up till now ?


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
DharmaRao Edula
Greenhorn

Joined: Feb 28, 2008
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

Joined: Feb 19, 2008
Posts: 2902
    
    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 :


Your code


Note : pl use code tag to surround you code snippet !!
DharmaRao Edula
Greenhorn

Joined: Feb 28, 2008
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

Joined: Feb 19, 2008
Posts: 2902
    
    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

Joined: Jun 24, 2008
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

Joined: Jul 01, 2008
Posts: 1
[ UD: Like Jesper says below, LetThemDoTheirOwnHomework ]
[ July 02, 2008: Message edited by: Ulf Dittmer ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Seamus Minogue
Ranch Hand

Joined: Jun 24, 2008
Posts: 41
Apologies...
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Seamus Minogue:
Apologies...


Why you are saying this???

were you "Josh Burgans"


The Best way to predict your future is to create it - Every great individual common man
Seamus Minogue
Ranch Hand

Joined: Jun 24, 2008
Posts: 41
No but it seemed like I made the same mistake by not just providing the answer.

*shrug*
 
 
subject: help in writing programme