# how to use nested for loop

• 12-17-2013, 06:06 PM
geekgeek1
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 ?
• 12-17-2013, 06:19 PM
gimbal2
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.
• 12-18-2013, 07:22 PM
geekgeek1
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.
• 12-18-2013, 07:53 PM
JosAH
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
• 12-18-2013, 07:54 PM
Norm
Re: how to use nested for loop
What do you want the code to print out?
• 02-04-2014, 07:16 AM
Paru
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.