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 :'(

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.