Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Nested Loop Counter

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really need some help with this programming assignment. I am suppose to ask a user for a number and from there you are suppose to output the counter in rows of 5 numbers per row.

1 2 3 4 5
6 7 8 9 10
11 12 13 14 and so on I am stuck on the coding part of this this is what I have so far.

// This program is a nested loop that takes in a whole number and counts to what ever number you
// would like it to and puts the numbers into columns.

// Sarah Creech
// October 18, 2004

public class NestedLoop
public static void main(String[] args)

int ctr = 1;
int sum;

System.out.println("Please enter any whole number that you would like me to count to.");

sum = SavitchIn.readLineInt();

System.out.print(ctr + " ");
} while(ctr <= sum);

Anyones help will be appreciated.
lowercase baba
Posts: 12839
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, from the subject of this post, you probably know you need some kind of loop inside another loop.

If i were you, i'd not try and do it all at once. You've got the input working, i assume. that's a good start.

now look at what you need to output... see if you can analyze it for any patterns...

what i see is that the first number of every line increments by 5 each time. Can you write a single loop that only prints out the

etc. up to your input number? try and get that to work first. once you have that working, we can work on the next part...
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Sarah -

I'd like to recommend that you start your design with pseudocode, not with coding. If you can make your design work with pseudocode, you'll be able to "port" it to Java. For example, a nested loop could look like this:

Using pencil and paper, you can see that your output will be:

11 12 13 14 15
21 22 23 24 25 etc.

Now, once you have the pseudocode working, build your Java code from it.

See how that follows the pseudocode? You could even use your pseudocode as comments for the Java, but I'll leave that up to you to work out.

Let me know if that helps, or if you need more.
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sarah Creech
You can take a look with a similar topic that we have discussed before, I predict that you may know each other.
You can click here
Of course, some rachers gave you some tips and you just require to edit a little bit code that will become an answer.
My suggestion will be that keep the program as simple as possible.
Do you think that a loop is enough?

[ October 18, 2004: Message edited by: siu chung man ]
Sarah Creech
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys for all of the help I finally got it working.
I did not know that anyone had replied because it usually emails me and notifies you when someone does answer your question. Dont know why it did not this time. Thanks everyone for your help.
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
    Bookmark Topic Watch Topic
  • New Topic