•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Nested For Loop [EXAMPLES!]

nested loop for loop loop

121 replies to this topic

### #13 amaliwa

amaliwa

CC Lurker

• Just Joined
• 1 posts

Posted 21 August 2010 - 08:51 AM

how can i write a java programe to display this
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
• 0

### #14 wim DC

wim DC

Roar

• Expert Member
• 2681 posts
• Programming Language:Java, JavaScript, PL/SQL
• Learning:Python

Posted 21 August 2010 - 10:13 AM

```
for( int i=1, number=1 ; i<=5 ; i++ ){

for( int j=1 ; j<=i ; j++, number++ ){

System.out.print(number + " ");

}

System.out.println("");

}```

• 0

### #15 Veronica

Veronica

CC Lurker

• Just Joined
• 3 posts

Posted 21 August 2010 - 08:52 PM

pls help~ i cant write the correct codes for the following output:
a. 1
21
321
4321
54321
654321

b.
123456
12345
1234
123
12
1
• 0

### #16 Veronica

Veronica

CC Lurker

• Just Joined
• 3 posts

Posted 21 August 2010 - 08:56 PM

pls help~ i cant write the correct codes for the following output:
a.
- - - - 1
- - - -21
- - -321
- - 4321
- 54321
654321

b.
123456
12345
1234
123
12
1

correction: the - in a is space
• 0

### #17 wim DC

wim DC

Roar

• Expert Member
• 2681 posts
• Programming Language:Java, JavaScript, PL/SQL
• Learning:Python

Posted 21 August 2010 - 11:11 PM

correction: the - in a is space

```for(int i=6 ; i>0 ; i--){
for(int j=1 ; j<i ; j++){
System.out.print(" ");
}
for(int k=7-i ; k>0 ; k--){
System.out.print(k);
}
System.out.println("");
}

System.out.println("");

for(int i=6 ; i>0 ; i--){
for(int j=1 ; j<=i ; j++){
System.out.print(j);
}
System.out.println("");
}```

• 0

### #18 Veronica

Veronica

CC Lurker

• Just Joined
• 3 posts

Posted 22 August 2010 - 04:25 AM

```for(int i=6 ; i>0 ; i--){
for(int j=1 ; j<i ; j++){
System.out.print(" ");
}
for(int k=7-i ; k>0 ; k--){
System.out.print(k);
}
System.out.println("");
}

System.out.println("");

for(int i=6 ; i>0 ; i--){
for(int j=1 ; j<=i ; j++){
System.out.print(j);
}
System.out.println("");
}```

Thanks alot
• 0

### #19 namesNick

namesNick

CC Lurker

• Just Joined
• 1 posts

Posted 24 September 2010 - 07:25 PM

Times table.

```                int mLength1 = 4;
int mLength2 = mLength1;
for (int i = 1; i <= mLength1; i++) {
for (int j = 1; j <= mLength2; j++) {
System.out.print(j * i + "  ");
}
System.out.println();```

output:

1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
• 0

### #20 charmanderxd

charmanderxd

CC Lurker

• Just Joined
• 1 posts

Posted 01 October 2010 - 10:26 AM

Hi guys i cant figure out this problem.
----1
---22
--333
-4444
55555

i tried everything within my knowledge and book. So if you guys can give me some hints >_< that would be nice. (i want to write it myself so i can learn.)

So far i know how to produce this code.
1
22
333
4444
55555

should i use the same one and edit it? or is it going to be completely different? using System.out.print (" ")
• 0

### #21 karenaxace

karenaxace

CC Lurker

• Just Joined
• 1 posts

Posted 02 October 2010 - 07:55 PM

how to do this
55551
55512
55123
51234
12345

Edited by dargueta, 28 April 2013 - 08:41 PM.
Fixed emoticons

• 0

### #22 Alexander

Alexander

YOL9

• Moderator
• 3963 posts
• Location:Vancouver, Eh! Cleverness: 200
• Programming Language:C, C++, PHP, Assembly

Posted 02 October 2010 - 08:34 PM

Hi guys i cant figure out this problem.
----1
---22
--333
-4444
55555
[...]
should i use the same one and edit it? or is it going to be completely different? using System.out.print (" ")

There is String.format() for that, there's much documentation on the formats but here is a quick example:
```String str = "1";
str = String.format("%5s", str).replace(' ', '-');```
This will pad "1" with dashes, making "----1". "%5s" is basically "format a string, pad into 5 characters"

So 22 will be ---22 and 333 will be --333 etc.
• 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.

### #23 BenzR

BenzR

CC Lurker

• New Member
• 3 posts

Posted 10 October 2010 - 04:29 AM

i am a rookie from java...i find it is quite hard for me:crying:
i wanna know how to print out like this?

88888881
88888812
88888123
88881234
88812345
88123456
81234567
12345678

could someone tell me the solution?
• 0

### #24 Alexander

Alexander

YOL9

• Moderator
• 3963 posts
• Location:Vancouver, Eh! Cleverness: 200
• Programming Language:C, C++, PHP, Assembly

Posted 10 October 2010 - 04:35 AM

@BenzR: Please create a new thread in the Java Help section to get more help on this. You're hijacking a thread!
• 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.

### Also tagged with one or more of these keywords: nested loop, for loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download