Results 1 to 6 of 6
  1. #1
    geekgeek1 is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Post 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. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    geekgeek1 is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default 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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: how to use nested for loop

    What do you want the code to print out?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Paru is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default 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.

Similar Threads

  1. Nested for loop problem.
    By Hello in forum New To Java
    Replies: 7
    Last Post: 11-03-2011, 05:06 PM
  2. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 05:10 PM
  3. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 03:39 AM
  4. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 02:49 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM

Tags for this Thread

Posting Permissions

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