I'm trying to use for loops to output:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

public class Fit1
{
    public static void main(String args[])
    {
    int side1,side2, side3;

        for (side1 = 1; side1 <= 3; side1++) 
        {
            for (side2 = 2; side2 <= 3; side2++) 
            {


                for (side3 = 3; side3 <= 3; side3++) 
                {

                System.out.println(side1 + " " + side2 + " " + side3 + " ");

                }

            }


        }

    }
}

Recommended Answers

All 2 Replies

for (side3 = 3; side3 <= 3; side3++)

has no effect whatsoever other than setting side3 equal to 3. As far as looping, it does nothing.

Interesting problem! You are looking to print out the set of permutations of the three numbers 1,2,3. I did not know where to start initially but soon seen what was needed once I saw your approach. You are on the right lines but the above code will not work.

Rather than just present my solution I would make the following comments;

The for loops listed always start after the first value of the outer loop, but you need to switch to lower values for the later rows, e.g. 3,2,1.

Indeed each variable needs to take on each value at some stage so the latter loops need to run from 1 to 3.

If you just ran the loops with this change you get duplications 1,1,2 or 1,3,1 etc. so you will need to filter out those combinations where there are duplicates and print only side1, side2, and side3 when they are all different!

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.