Nested for loop help

need help printing nested for loop properly

Page 1 of 1

4 Replies - 857 Views - Last Post: 10 November 2009 - 12:07 PM Rate Topic: -----

#1 redtea  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 07-May 09

Nested for loop help

Posted 10 November 2009 - 11:40 AM

To start off the first number is always a multiple of 10 and the last number will always end with a 9. So a proper input would be 10-49.

The numbers must look like this went printed:
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49


This is my code and my output is as follows:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
20 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
30 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
40 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

Any guidance is greatly appreciated.

 class NumberTable {
   
	public static void main (int first, int last) {
		
		for (int j = first; j <= last; j+= 10) {
		  System.out.print(" " + j);

		  for (int i = first + 1; i <= last; i++) {
			System.out.print(" " + i);
		  
		  }
			
			System.out.println(" ");
			
		}
	   
	}
}

This post has been edited by redtea: 10 November 2009 - 11:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Nested for loop help

#2 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: Nested for loop help

Posted 10 November 2009 - 11:46 AM

You can do this in really clever way :)

for(int i=1;i<10;i++)
for(int j=0;j<10;j++)
System.out.print(i +j +" ");


I hope you get the drift im on.
Was This Post Helpful? 0
  • +
  • -

#3 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Nested for loop help

Posted 10 November 2009 - 11:48 AM

 class NumberTable {
   
	public static void main (int first, int last) {
		
		for (int j = first; j <= last; j+= 10) {
		  System.out.print(" " + j);

		  for (int i = first + 1; i <= last; i++) {
			System.out.print(" " + i);
		  
		  }
			
			System.out.println(" ");
			
		}
	   
	}
}



Think about it this way,
your first for loop starts at ten, prints it, then immediately enters your second loop, where i = first + 1 then runs all the way through to the last number. After this, your first loop increments by ten, prints 20, then enters the second loop where i = first + 1 again then runs all the way through to your variable, last. See if you can figure it out now. :D

View PostLuckless, on 10 Nov, 2009 - 10:47 AM, said:

 class NumberTable {
   
	public static void main (int first, int last) {
		
		for (int j = first; j <= last; j+= 10) {
		  System.out.print(" " + j);

		  for (int i = first + 1; i <= last; i++) {
			System.out.print(" " + i);
		  
		  }
			
			System.out.println(" ");
			
		}
	   
	}
}



Think about it this way,
your first for loop starts at ten, prints it, then immediately enters your second loop, where i = first + 1 then runs all the way through to the last number. After this, your first loop increments by ten, prints 20, then enters the second loop where i = first + 1 again then runs all the way through to your variable, last. See if you can figure it out now. :D

...or the answer can be posted before I finish my lecture. lol
Was This Post Helpful? 1
  • +
  • -

#4 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: Nested for loop help

Posted 10 November 2009 - 11:51 AM

LOL sorry the code i gave actually adds i and j what i didnt expect as i added string in the end so its better to do this way.
		for(int i=1;i<10;i++)
		{
			for(int j=0;j<10;j++)
				System.out.print(String.valueOf(i) +String.valueOf(j) +" ");
			System.out.println();
		}


Hope this helps :D
Was This Post Helpful? 0
  • +
  • -

#5 redtea  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 07-May 09

Re: Nested for loop help

Posted 10 November 2009 - 12:07 PM

Thanks Luckless I got it I believe.

class NumberTable {
   
	public static void main (int first, int last) {
	   
		for (int j = first; j <= last; j+= 10) {
		  System.out.print(" " + j);

		  for (int i = j + 1; i <= j + 9; i++) {
			
			System.out.print(" " + i);
		  
		  }
			
			System.out.println(" ");
			
		}
	   
	}
}

This post has been edited by redtea: 10 November 2009 - 12:07 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1