I have a program that I need to print out a in the following manner:

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

I have figured out how to print them out, but not break the lines up as so. I also need the program to be able to handle a range of values not just these. I know I can do this as a nested for loop. I am just not sure how... Any help would be appreciated!

class numTable {
	public static void main (String args[]){
		for (int i=10; i < 50; i++){
			switch (i){
			case 19:
			case 29:
			case 39:
		System.out.println (" " + i);
			break;
		default: System.out.println(" " +i);
			
	}
     }
   }
}

Recommended Answers

All 3 Replies

To accept a larger amount of values would I increase this part:

for (int i=10; i < 50; i++){

to this:

for (int i = 10; i < 100; i++){

The nested loop can be written as follows:

import java.lang.*;
public class NumberPrinted{ 

public static void main(String args[]){	
  int n = Integer.parseInt(args[0]);
  int number = 10;
  for (int i = 0; i< n; i++){
  for (int j=0; j<10; j++)
  System.out.printf("%4d", number++);
  System.out.println();
		}
	}
}

Using C-style "printf("%4d", a)" to print an number 'a' has the advantage in easily controlling the spaces.
On DOS window the following output may be shown:
java NumberPrinted 9
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
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99

Fantastic! Thank you so much for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.