This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help in writing programme

 
DharmaRao Edula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show your effort first . Where you get stuck ? Post your code which you wrote up till now ?
 
DharmaRao Edula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ UD: Like Jesper says below, LetThemDoTheirOwnHomework ]
[ July 02, 2008: Message edited by: Ulf Dittmer ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies...
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Seamus Minogue:
Apologies...


Why you are saying this???

were you "Josh Burgans"
 
Seamus Minogue
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No but it seemed like I made the same mistake by not just providing the answer.

*shrug*
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic