write a program to print out the numbers 10 through 49 in the following manner



questions and answers For computer science :

write a program to print out the numbers 10 through 49 in the following manner



Write a program to print out the numbers 10 through 49 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
How would you do it? Here is an example of poorly written code:
for ( int i = 10; i < 50; i++ ) {
     switch (i) {
     case 19:
     case 29:
     case 39:
System.out.println(" " + i); // move to the next line
           Break;
     default: System.out.print(" " + i);
     }
   }
This code is not written well because it works only for printing 10 through 49. Try to develop the code so that it can be extended easily to handle any range of values. You can do this coding in two ways: with a nested-for statement or with modulo arithmetic. (If you divide a number by 10 and the remainder is 9, then the number is 9, 19, 29, or 39, and so forth.)

0 comments:

Post a Comment