Greetings! I was given a task yesterday to constuct a table like this:

5 4 3 2 1
4 5 4 3 2
3 4 5 4 3
2 3 4 5 4
1 2 3 4 5

using loops and only loops. I've been trying to solve this for hours and still no luck in finding its solution. can anyone help me? pleaseeeeeeee :'(

Dani AI

Generated

A simple, compact way to generate that table is to compute each cell from its row and column rather than tracking state or filling an auxiliary grid. For an N x N table the value at row i and column j (0-based) is:

val = N - Math.abs(j - i)

That single expression produces the peak N on the diagonal that shifts each row, and the numbers fall off symmetrically.

int N = 5;
for (int i = 0; i < N; i++) {
    StringBuilder line = new StringBuilder();
    for (int j = 0; j < N; j++) {
        int val = N - Math.abs(j - i);
        if (j > 0) line.append(' ');
        line.append(val);
    }
    System.out.println(line.toString());
}

This keeps the logic clear and removes temporary flags or manual up/down toggles. It also generalizes: change N and the same loops work. As noted, filling an array first is a readable alternative if you need to reuse the matrix later; the formula approach is lighter when you just need to print. Use a StringBuilder (as above) or printf for aligned columns when N can grow, and remember the algorithm is O(N^2) time and O(1) extra memory for streaming output.

Recommended Answers

All 5 Replies

Post your code and we will help you correct it, but no one is just going to give it to you.

As you can see for each line, you have a start, then increase each element by 1 and when you reach 5, if you didn't insert 5 elements in the line start reducing.

5 4 3 2 1(start)

4 5(reached 5, will decrease until the line is filled) 4 3 2(start)

3 4 5(reached 5, will decrease until the line is filled) 4 3(start)

Here's my code..
it doesnt do much...yet

public class LogicPuzzle1 {

    public static void main (String [] args)
    {
    	final int num = 5;
    	int ctr;
    	int ctr2;
    	int temp=0;
    	for(ctr=num;ctr>0;ctr--)
    	{
    		System.out.print(ctr);
    		if(ctr!=num)
    		{
    			temp=ctr;
    			if(temp==num)
    			{
    				for(ctr2=1;ctr2<num;ctr2++)
    				{
    					temp--;
    					System.out.print(temp);
    				}
    			}
    			else
    			{
    				for(ctr2=1;ctr2<num;ctr2++)
    				{
    					temp++;
    					System.out.print(temp);
    				}
    			}
    		}
    		else
    		{	
    			for(ctr2=1;ctr2<num;ctr2++)
    				System.out.print(ctr-ctr2);
    		}
    		System.out.println();
    	}
    }
    
}

THANK YOU javaAddict!!!!!!! Weeeee....
can anyone help me make my code more efficient? (remove uneeded stuff)

public class LogicPuzzle1 {

    public static void main (String [] args)
    {
    	final int num = 5;
    	int ctr;
    	int ctr2;
    	int temp=0;
    	boolean reachMax=false;
    	for(ctr=num;ctr>0;ctr--)
    	{
    		ctr2=num;
    		temp=ctr-1;
    		reachMax=false;
    		while(ctr2>0)
    		{
    			if(temp==5||reachMax==true)
    			{
    				reachMax=true;
    				temp--;
    			}
    			else
    			{
    				temp++;
    			}
    			System.out.print(temp);
    			ctr2--;
    		}
    		
    		System.out.println();
    	}
    }
    
}

There are many ways to implement this, so if it works don't worry about it. In my idea I was thinking of creating an array 5x5, and put the values in it before printing them. I think that it would have been simpler because with the array I could start putting things at the end of the array:
arr[0][4] = 1
arr[0][3] = 2 . . .
So instead of while inside the first for-loop, I would have the outside loop counting the line of the array, and the inside loop counting backwards and start putting the values at the end of the array.

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.