# i need help

• 10-14-2012, 08:49 AM
JAM1996
i need help
Write a program to output the intergers 1, 2, 3,4,…25, five to a line via nested loops.
Sample Output:

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
• 10-14-2012, 08:52 AM
eRaaaa
Re: i need help
ok, and where exactly do you need help?how is your approach?
• 10-14-2012, 08:54 AM
JAM1996
Re: i need help
writing it all
• 10-14-2012, 09:05 AM
eRaaaa
Re: i need help
brazenly....

Code:

```loop from i=1 to 25   print i   if i modulo 5 equal 0 then print linebreak```

that`s all....

/edit: oh i see...with nested loops..ok but it`s similar :)
• 10-14-2012, 10:01 AM
JAM1996
Re: i need help
can u not give me the full code????
• 10-14-2012, 10:57 AM
pbrockway2
Re: i need help
This isn't a code writing service.

But something appropriate might be along these lines:

Code:

```HAI CAN HAS STDIO? I HAS A VAR IM IN YR LOOP     UP VAR!!1     VISIBLE VAR N " "     IZ LEFTOVERZ VAR!!5 LIEK 0?         YARLY             VISIBLE "\n"     KTHX IM OUTTA YR LOOP KTHXBYE```
• 10-16-2012, 12:19 AM
behedwin
Re: i need help
ok i found this as a fun task to try and solve...
maybe i dident understand what you where asking for... but here is what i made :)

Code:

```class santa{       public static void main(String[] args) {             for(int counter=1;counter<=5;counter++)                     System.out.print(counter + " ");             System.out.println("");             for(int counter=6;counter<=10;counter++)                     System.out.print(counter + " ");             System.out.println("");             for(int counter=11;counter<=15;counter++)                     System.out.print(counter + " ");             System.out.println("");             for(int counter=16;counter<=20;counter++)                     System.out.print(counter + " ");             System.out.println("");             for(int counter=21;counter<=25;counter++)                     System.out.print(counter + " ");             System.out.println("");                               }  }```
• 10-16-2012, 01:41 AM
pbrockway2
Re: i need help
Begin classes with a capital letter (Santa) and most (right thinking) people consider it a good idea to use braces with for loops, even if they have one line bodies.

The OP's home^H^H^H^Hexercise called for nested loops. A pair of nested loops might do it: the outer one being the line you are printing and the inner one the position you are up to within the line.

---

The OP also wanted this written in JavaScript, not Java. But don't let that get in the way of your fun!
• 10-16-2012, 09:59 AM
behedwin
Re: i need help
Ups, sry. Missed that :)

I had fun learning to solve this in my way. Sry for stealing the thread hehe.

My biggest problem was to figure out how to do a line break.
It seems like a waste to make a System.out.println(" "); just to make a line break and then start with a new loop on next line.
Instead i wanted to just make one loop and tell it to break line after x amount of numbers.
• 10-16-2012, 10:49 PM
pbrockway2
Re: i need help
Quote:

Sry for stealing the thread hehe
That's OK - I don't think the thread is serving any other useful purpose.

Check out System.out.println() without any argument (nothing at all within the parentheses).

Quote:

i wanted to just make one loop and tell it to break line after x amount of numbers
That's what I did with the LOLCODE in #7. Do you know about the remainder operator (%) because it comes in handy here.

 I've just noticed that the loop in #7 never actually ends...
• 10-16-2012, 11:18 PM
behedwin
Re: i need help
ive seen % before... but seen it being used as a math value like *, / or + and -.
not sure how it would help here?

im probably missing something major here...
but i had this idea that i could take

public static void main(String[] args) {
for(int counter=1;counter<=25;counter++)
System.out.print(counter + " ");

and just ad somewhere that i want java to print out the sequence 1-25
and make a line break every 5 value or something.

instead of having to write all that code just becouse i want it to start at a new line
• 10-17-2012, 01:23 AM
pbrockway2
Re: i need help
Quote:

make a line break every 5 value
The % operator evaluates the remainder when a number is divided. (that's what the link was supposed to explain). So 3%5=3, 17%5=2 and - importantly - 5%5=0, 10%5=0, 15%5=0 etc. So it looks very much like you should be checking:

Code:

```if(counter % 5 == 0) {     // new line goes here }```