nested loop question

here's one for all you java gurus:

//import java.util.*;

publicclass RowsofTen

{

//static Scanner console = new Scanner(System.in);

publicstaticvoid main (String[] args)

{

int i, j;

for ( i = 11; i <= 40; i++)

{

for (j = i; j <= i + 9; j++)

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

System.out.println();

}

}

}

the output is this:

11 12 13 14 15 16 17 18 19 20

12 13 14 15 16 17 18 19 20 21

13 14 15 16 17 18 19 20 21 22

14 15 16 17 18 19 20 21 22 23

15 16 17 18 19 20 21 22 23 24

16 17 18 19 20 21 22 23 24 25

17 18 19 20 21 22 23 24 25 26

18 19 20 21 22 23 24 25 26 27

19 20 21 22 23 24 25 26 27 28

20 21 22 23 24 25 26 27 28 29

21 22 23 24 25 26 27 28 29 30

22 23 24 25 26 27 28 29 30 31

23 24 25 26 27 28 29 30 31 32

24 25 26 27 28 29 30 31 32 33

25 26 27 28 29 30 31 32 33 34

26 27 28 29 30 31 32 33 34 35

27 28 29 30 31 32 33 34 35 36

28 29 30 31 32 33 34 35 36 37

29 30 31 32 33 34 35 36 37 38

30 31 32 33 34 35 36 37 38 39

31 32 33 34 35 36 37 38 39 40

32 33 34 35 36 37 38 39 40 41

33 34 35 36 37 38 39 40 41 42

34 35 36 37 38 39 40 41 42 43

35 36 37 38 39 40 41 42 43 44

36 37 38 39 40 41 42 43 44 45

37 38 39 40 41 42 43 44 45 46

38 39 40 41 42 43 44 45 46 47

39 40 41 42 43 44 45 46 47 48

40 41 42 43 44 45 46 47 48 49

when i need the output to look like:

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

[2197 byte] By [nsfoura] at [2007-11-27 5:05:18]
# 1
Have you written these loops yourself? Do you have any understanding of how they work? If so you can easily get your desired result:Just change the interval of the outer loop from 1 to 10.If you don't know how to do that, get some basic tutorial on Java programming.
tom_jansena at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 2

public class RowsOfTen

{

//static Scanner console = new Scanner(System.in);

public static void main (String[] args)

{

int i, j;

for ( i = 11; i <= 40; i++)

{

for (j = i; j <= i + 9; j++) {

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

break;

}

}

}

}

but the output in a single line

AnanSmritia at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 3

Try this code

public class RowsOfTen

{

public static void main (String[] args)

{

int i, j;

int k=11;

for ( i = 1; i <= 3; i++)

{

for (j = 10; j>0; j--) {

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

k++;

}

System.out.println(" ");

}

}

}

b.m.krajua at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 4

if you just want the output, here is a simple way to do it

however if you are keen on using nested loops, you can try the solution given above

public class Test

{

public static void main(String [] a){

for(int i=11;i<=40;i++){

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

if(i%10==0)

System.out.println("");

}

}

}

Skeletora at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 5

public class RowsofTen

{

//static Scanner console = new Scanner(System.in);

public static void main (String[] args)

{

int i =0 , j =0;

for ( i = 1; i <4; i++)

{

for (j = (i*10+1); j <= (i*10+1) + 9; j++)

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

System.out.println();

}

}

}

There u go.... That should work just fine....

and @tom_jansen, changing the values of the outer loop will give u a weird answer.

-...icedT...-a at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 6

Your code works, happy to see you figured out a way.

When I said increase the outer loop interval, I mean you have an interval of 1, because you do i++ each time.

To have an interval of 10, do i+=10 each time.

for ( i = 11; i <= 40; i+=10)

for (j = i; j <= i + 9; j++)

tom_jansena at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 7

@OP: The mistake you made in your example is assuming the value of i is changed when you use it in the inner loop. As you can clearly see from your output i is incremented by 1 per loop, not by 10. Either do any of the previously proposed things or replace the i++

in the outer loop by i+=10

Edit: Tom can you borrow (or is it lend) me your speed-typing course...

Message was edited by:

Peetzore

Peetzorea at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 8

Hi dude

its very simple , just try this out :

public class Test8

{

//static Scanner console = new Scanner(System.in);

public static void main (String[] args)

{

int i, j;

for ( i = 11; i <= 40; i++)

{

for (j = i; j <= i + 9; j++)

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

i=j-1;

System.out.println();

}

}

}

sb.majumder_07a at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 9

public class RowsofTen

{

public static void main (String[] args){

int i;

int j;

for ( i = 1; i <= 3; i++)

{

for (j = 1; j <= 10; j++) { System.out.print((i*10)+j + " "); }

System.out.println();

}

}

}

That should do the trick!

Message was edited by: (changed 9->10)

gueritol

gueritola at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 10
Same problem, different answers. Cool, even though the problem seems simple.@tom , yours look the neatest... :)
-...icedT...-a at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...
# 11
Try thisSystem.out.println("11 12 13 14 15 16 17 18 19 20");System.out.println("21 22 23 24 25 26 27 28 29 30");System.out.println("31 32 33 34 35 36 37 38 39 40");err...Sorry..
sridhar_rreddya at 2007-7-12 10:23:45 > top of Java-index,Java Essentials,New To Java...