Connecting Tech Pros Worldwide Forums | Help | Site Map

Nested For-Loop Problem...Driving me NUTS

Newbie
 
Join Date: Sep 2007
Posts: 2
#1: Sep 29 '07
Hello, I need to reproduce this pattern using one main for loop and one nested for loop.

0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210

And here is what I coded, but I can't seem to make it the right way.

Expand|Select|Wrap|Line Numbers
  1. for (int x = 0; x < 10; x++)
  2.          {
  3.              for (int y = 0; y < x; y++)
  4.              {
  5.                  System.out.print(y);
  6.              }
  7.              System.out.println();
  8.          }

Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Sep 29 '07

re: Nested For-Loop Problem...Driving me NUTS


What output does that give you?
Newbie
 
Join Date: Sep 2007
Posts: 2
#3: Sep 29 '07

re: Nested For-Loop Problem...Driving me NUTS


0
01
012
0123
01234
012345
0123456
01234567
012345678

This is what output I get. Close, but no cigar.
hsn's Avatar
hsn hsn is offline
Familiar Sight
 
Join Date: Sep 2007
Location: Dubai-UAE
Posts: 237
#4: Sep 29 '07

re: Nested For-Loop Problem...Driving me NUTS


this is a way to answer your quesion
<CODE REMOVED. Please read our posting guidelines, especially the section about responding to assignments. .

MODERATOR>
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#5: Sep 29 '07

re: Nested For-Loop Problem...Driving me NUTS


Quote:

Originally Posted by tipster3000

0
01
012
0123
01234
012345
0123456
01234567
012345678

This is what output I get. Close, but no cigar.

OK, it looks like you have half of it down. Now, what can you change about your loops to change the order in which these numbers appear?

HINT: You've seen i++...ever hear of i--? It subtracts 1 from i every time it executes, much like i++ adds one to i every time it executes.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Sep 29 '07

re: Nested For-Loop Problem...Driving me NUTS


Quote:

Originally Posted by tipster3000

0
01
012
0123
01234
012345
0123456
01234567
012345678

This is what output I get. Close, but no cigar.

Have a close look at both loops when they get executed for the first time: x == 0.
The inner loop doesn't print anything at all. Change that '<' sign to '<=' and see
what happens then. I know what happens: the correct number of rows are printed
but the numbers are still wrong: i.e. the digits are printed in increasing order but
you want them in decreasing order. Figure it out.

kind regards,

Jos
Reply