# Thread: how to use nested for loop

1. Member
Join Date
Dec 2013
Posts
2
Rep Power
0

## how to use nested for loop

Question: you are only allowed to use numbers from 1-6. Write a program to find all the permutations when three numbers are multiplied together to give a result 8. one number cannot occur twice in any permutation.

public class number4
{
public static void main(String[] args)
{
for(int a=1; a<=4; a++)
{
for(int b=1; b<=4; b++)
{
for(int c=1; c<=4; c++)
{
if(a*b*c==8)
{
System.out.println(a+ " " +b+ " " +c);
}
}
}
}
}
}

my program also prints out 2 2 2. but i'm not allowed to do that. how can I stop it from printing 2 2 2 ?

2. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
2,624
Rep Power
3

## Re: how to use nested for loop

By writing the code that prevents it. Now you have to think about how to do that. That's basically the whole point of the assignment: you have to come up with the solution for it, you cannot go to a forum and ask other people to do the thinking for you because then this is all for nothing.

So reboot. This is not a problem. This is not a chore. This is not homework. It is in fact a fun puzzle to solve and I would highly suggest you NOT start with code on this one; try to write it out on paper first. I usually stink at numbers puzzles so I actually write out all the possibilities that are valid and invalid and then try to find a pattern in there that I can translate to simple code rules.

3. Member
Join Date
Dec 2013
Posts
2
Rep Power
0

## Re: how to use nested for loop

i already did all the coding. its just the bit i dont understand. its not like im asking for the whole solution. if you dont have the ability to answer it then just dont comment and waste your and my time you idiot.

4. ## Re: how to use nested for loop

@geekgeek1: please change your attitude; Gimbal's reply was only reasonable; you problem is a simple coding problem.

Jos

5. ## Re: how to use nested for loop

What do you want the code to print out?

6. Member
Join Date
Feb 2014
Posts
1
Rep Power
0

## Re: how to use nested for loop

Just include additional check for uniqueness in the if statement

If ( a*b*c=8) && (a!=b||c) && ( check for b and c like a)

Shud work I think - hope this helps.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•