•

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 Loops

loop

9 replies to this topic

### #1 Confused

Confused

CC Lurker

• New Member
• 3 posts
• Programming Language:Java, JavaScript
• Learning:Java, JavaScript

Posted 28 September 2012 - 07:24 AM

How to do this?????

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
• 0

### #2 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 28 September 2012 - 08:25 AM

Some day I'm going to write a theorem to predict the frequency of nested loop questions appearing on this forum. (kidding...)

Okay, this is simple when you work it out. Tell me, what's changing from line to line? What's the pattern for each line? (I don't want to give you the answer straight out. I want to help you discover it. It'll be more meaningful to you that way.)

EDIT: I think your formatting may have gotten messed up. Did you mean for your desired output to resemble this? (right-justified)
```           1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
```

• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### #3 kernelcoder

kernelcoder

CC Devotee

• Expert Member
• 990 posts
• Location:Dhaka
• Programming Language:C, Java, C++, C#, Visual Basic .NET
• Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 28 September 2012 - 08:31 AM

I suggest you to study this post -- specially the table she shows there. Your problem is somewhat similar to her. I think it will help you to solve your problem.
• 0

### #4 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 28 September 2012 - 08:33 AM

Also this one.
• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### #5 Confused

Confused

CC Lurker

• New Member
• 3 posts
• Programming Language:Java, JavaScript
• Learning:Java, JavaScript

Posted 01 October 2012 - 07:41 AM

Yes it got messed up. Actually it supposed to be like this:

Pattern1

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1

Pattern 2

1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

I've tried doing it by myself,but i just can't get it right. Hope someone can help me.........

sorry,i just can't get the numbers in order....
• 0

### #6 kernelcoder

kernelcoder

CC Devotee

• Expert Member
• 990 posts
• Location:Dhaka
• Programming Language:C, Java, C++, C#, Visual Basic .NET
• Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 01 October 2012 - 07:57 AM

.......
I've tried doing it by myself,but i just can't get it right. Hope someone can help me.........

sorry,i just can't get the numbers in order....

Can you show us what you have tried so far so that we can help you about what you are doing wrong?
• 0

### #7 Confused

Confused

CC Lurker

• New Member
• 3 posts
• Programming Language:Java, JavaScript
• Learning:Java, JavaScript

Posted 01 October 2012 - 08:18 AM

This is for pattern 1.....

public class Loop3{
public static void main(String args [])

{
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( " " );

}

}

}

How to add space in between the numbers??? i don't know how to write a coding for pattern 2. Hope you can help me. Tq
• 0

### #8 TALucas

TALucas

• 118 posts
• Programming Language:Java, (Visual) Basic, Visual Basic .NET, Transact-SQL
• Learning:C++, PHP, ActionScript

Posted 01 October 2012 - 08:30 AM

In order to add a space in between each number and also keep them in line, you would need to increase the space in the top print statement, and add a space after the "k" variable in the second print statement.

```public class Loop3{
public static void main(String args [])

{
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( " " );

}

}

}```

• 0

### #9 kernelcoder

kernelcoder

CC Devotee

• Expert Member
• 990 posts
• Location:Dhaka
• Programming Language:C, Java, C++, C#, Visual Basic .NET
• Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 01 October 2012 - 05:29 PM

This is for pattern 1.....

```public class Loop3{
public static void main(String args [])

{
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( " " );

}

}

}
```
How to add space in between the numbers??? i don't know how to write a coding for pattern 2. Hope you can help me. Tq

Excellent! You made it! You see, you are just only missing a space character to print? Now you are printing only one space but you need to print two space in the space printing loop (the first inner loop). Also you need to print another extra space after printing a number.

I bet you will be able to solve the second problem as well. Just a hint -- this time you need to print space in increasing order (starting from zero) and print numbers in decreasing numbers of digit with keeping increasing digit order. To do that you can iterate the outer loop from 1 to 6. Then print space from 0 to i (iterator of outer loop) in the first inner loop. Finally, print digit from 1 to i (the iterator of outer loop) in the second inner loop.

One other thing: please surround your code next time while you paste the code with the code tag so it will look good to read.
• 0

### #10 ShekharMishra

ShekharMishra

CC Lurker

• Just Joined
• 1 posts

Posted 08 September 2013 - 02:31 AM

CC Lurker

this is the solution to your pattern 2...

class loop

{

public static void main(String args[])

{

for(int i=6;i>=1;i--)

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

{System.out.print(j);

}System.out.println();

}

}

}

• -1

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

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