-1

I am trying to make a number grid using a Java for-loop but I have difficulty making it aligned and following the pattern.

The output that I want to see is:

5 4 3 2 1
6 7 8 9 10
15 14 13 12 11
16 17 18 19 20
25 24 23 22 21

I've been watching youtube tutorials and articles about Java for-loops but I can't find any idea to solve this. I am currently stuck with this code:

int j=5;
int up1=14, up2=6;

for(int u=1; u<=5; u++)
{
    for(int s=1; s<=5; s++)
    {
        System.out.print(j+"\t");
        j--;


    }
    System.out.println("");

    if(u%2==0){
        j+=up1;
    }else{
        j+=up2;
    }
}

Its output is:

5   4   3   2   1   
6   5   4   3   2   
15  14  13  12  11  
16  15  14  13  12  
25  24  23  22  21

I have heard about int update but I have no idea how to apply it in my code.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • I would start finding a pattern, – Aalexander Jan 08 '21 at 20:38
  • You are using the tab char `\t`. You could also do `System.out.printf("%3d", j);` – Joop Eggen Jan 08 '21 at 20:39
  • just use one space character between every print and a new line after the inner for loop – lordvidex Jan 08 '21 at 20:57
  • @JoopEggen `printf("%3d", j);` is for `c` programming not Java. I am talking about inner source code of parenthesis and printf. In java we don't use `comma` inside parenthesis mark. We only use addition symbol to add anything –  Jan 08 '21 at 21:01
  • @Istiak [`printf`](https://docs.oracle.com/javase/9/docs/api/java/io/PrintStream.html#printf-java.lang.String-java.lang.Object...-) also was added to java, and would here format a number to padded, right aligned 3 positions. With varargs, variable number of arguments after the format. – Joop Eggen Jan 08 '21 at 21:31

6 Answers6

1

You forgot to invert the increment(-1/+1) every line. Then you need only to adjust up1 and you're fine

    int j = 5;
    int inc = -1;
    int up1 = 4, up2 = 6;

    for (int u = 1; u <= 5; u++) {
        for (int s = 1; s <= 5; s++) {
            System.out.print(j + "\t");
            j += inc;

        }
        System.out.println("");

        inc = -inc;
        if (u % 2 == 0) {
            j += up1;
        } else {
            j += up2;
        }
    }

Output:

5   4   3   2   1   
6   7   8   9   10  
15  14  13  12  11  
16  17  18  19  20  
25  24  23  22  21

Your problem with the alignment might be that you want leading spaces for the single digits, then use this for the print:

    System.out.print(String.format("%2d\t", j));

Output

 5   4   3   2   1  
 6   7   8   9  10  
15  14  13  12  11  
16  17  18  19  20  
25  24  23  22  21  
Turo
  • 4,724
  • 2
  • 14
  • 27
0

Solution


Even Rows

  • The pattern for the even rows is that it increases always by 10, you can simply take your j indice and multiply it by factor 5 times the row number.

Important note Here in the example I started counting rows at zero 0 - 4, because first row is odd

Row || Column

     1.                   2.                   3.
0. 5 + (5*0) -> 5      4 + (5*0) -> 4        3 + (5*0) -> 3 
2. 5 + (5*2) -> 15     4 + (5*2) -> 14       3 + (5*2) -> 13  .... 
4. 5 + (5*4) -> 25     4 + (5*4) -> 24       3 + (5*4) -> 23 

Odd Rows

For the odd rows the pattern is more difficult It increases the last column by ten and then go "backwards" the j value. Here I defined an counter variable which I increment in every odd iteration.

Row || Column

        1.                      2.
1. 10*1 - 5 + 1 -> 6      10*1 - 4 + 1 -> 7
3. 10*2 - 5 + 1 -> 16     10*2 - 4 + 1 -> 17
5. 10*3 - 5 + 1 -> 26     10*3 - 4 + 1 -> 27 // this it how it would continue

Here the Full Solution

public static void main(String[] args) {
    int counter = 1;
    for(int i = 0; i < 5; i++) {
        for(int j = 5; j > 0; j--){
        if(i % 2 == 0) {
            System.out.print(j+(5*i)+ " ");
        }else {
            System.out.print(10*counter -j+1  + " ");
            
        }

        }
        if(i%2!=0) {
            counter ++;
        }
        System.out.println();
        
    }

Output is

5 4 3 2 1 
6 7 8 9 10 
15 14 13 12 11 
16 17 18 19 20 
25 24 23 22 21 
Aalexander
  • 4,987
  • 3
  • 11
  • 34
0

Easy way to say your pattern is correct. Now, you remove "tab" or spacing on output.

Look you wrote \t which means give me a spacing of "tab" size.

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

You just have to remove \t. Than, give it a single space.

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

Done!

0

The given pattern goes like this:

  1. The pattern starts with a number e.g. X = 5.
  2. When the main loop counter is an odd number, the print counter starts with start value and decreases by one X times. Finally, it sets start for the next row to start with the final value of the print counter - 1 + X.
  3. When the main loop counter is an even number, the print counter starts with start value and increases by one X times. Finally, it sets start for the next row to start with start + 1.

Based on this property of the pattern, you can write it as

public class Main {
    public static void main(String[] args) {
        final int X = 5;
        int start = X, j, k;
        for (int i = 1; i <= X; i++) {
            if (i % 2 == 0) {
                k = 1;
                for (j = start; k <= X; j++, k++) {
                    System.out.print(j + "\t");
                }
                start = j - 1 + X;
            } else {
                k = 1;
                for (j = start; k <= X; j--, k++) {
                    System.out.print(j + "\t");
                }
                start++;
            }

            System.out.println();
        }
    }
}

Output:

5   4   3   2   1   
6   7   8   9   10  
15  14  13  12  11  
16  17  18  19  20  
25  24  23  22  21  

Change the value of X to 10 and you will get the following pattern:

10  9   8   7   6   5   4   3   2   1   
11  12  13  14  15  16  17  18  19  20  
30  29  28  27  26  25  24  23  22  21  
31  32  33  34  35  36  37  38  39  40  
50  49  48  47  46  45  44  43  42  41  
51  52  53  54  55  56  57  58  59  60  
70  69  68  67  66  65  64  63  62  61  
71  72  73  74  75  76  77  78  79  80  
90  89  88  87  86  85  84  83  82  81  
91  92  93  94  95  96  97  98  99  100 
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0
int k = 1;
    for (int i = 1; i <= 5; i++) {
        String accending = "",descending="";
        for (int j = k; j < k+5; j++) {
            accending+=j+"\t";
            descending=j+"\t"+descending;
        }

        if (i % 2 != 0) {
            System.out.println(descending);
            k=Integer.parseInt(descending.split("\t")[0])+1;
        }else{
            System.out.println(accending);
            k=Integer.parseInt(accending.split("\t")[4])+1;
        }
    }
  • I think it is easy to understand with this code.Simply turn ending digit in each line to a number.
-1
int j=5;
int up1=14, up2=6;

for(int u=1; u<=5; u++)
{
  for(int s=1; s<=5; s++)
  {
    System.out.printf("%5d",j);
    j--;
  }

  System.out.println("");

  if(u%2==0) {
    j+=up1;
  } else {
    j+=up2;
  }
}

What do you think about this code?

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34