views:

425

answers:

7

Goal:
1234
2345
3456
4567
5678

i have the pattern down but it doesnt println after length(4):

    int i;
    int a;

    for (i = 1; i <= 5; i++) 
    {
      for (a = i;a<=i+3;a++)
      {
        System.out.print(a);
      }
    }

My output is: 12342345345645675678

+2  A: 
int i;
int a;

for (i = 1; i <= 5; i++) 
{
  for (a = i;a<=i+3;a++)
  {
    System.out.print(a);
  }
  System.out.println();
}
David Kanarek
+3  A: 

Just add it after the second loop:

int i;
int a;

for (i = 1; i <= 5; i++) {
  for (a = i;a<=i+3;a++) {
    System.out.print(a);
  }
  System.out.println();
}
OscarRyz
+3  A: 
int i;
int a;

for (i = 1; i <= 5; i++) 
{
  for (a = i;a<=i+3;a++)
  {
    System.out.print(a);
  }
  System.out.println(); // add this code
{
jjnguy
+2  A: 

Add System.out.println() after the inner loop.

Bozho
+2  A: 

Try:

int i;
int a;

for (i = 1; i <= 5; i++) {
    for (a = i;a<=i+3;a++) {

        System.out.print(a);
    }
    System.out.println(); // this will print a new line.
}
codaddict
+3  A: 

No need to have two for loops, try :

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

EDIT : I know this will limit the flexibility, but this is just a toy problem.

fastcodejava
+1  A: 

Add System.out.Println() after the inner loop. This will move the cursor to next line

Ram