Results 1 to 4 of 4
  1. #1
    DanK is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default Beginner Java question

    My teacher asked us what the value of product is after this:

    int [ ] factors = {2, 3, 4, 7, 2, 5};
    int product = 1;
    for(int i =1; i < factors.length; i+=2)
    {
    product *= (factors [i] % factors [i - 1]);
    }




    He told me the answer is 3, but i was wondering why it isnt 1?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why guess or wonder? Just adjust your program to let Java show you why it is so:
    Java Code:
    public class Fu
    {
      public static void main(String[] args)
      {
        int[] factors =
        {
          2, 3, 4, 7, 2, 5
        };
        int product = 1;
        for (int i = 1; i < factors.length; i += 2)
        {
          int firstTerm = factors[i];
          int secondTerm = factors[i - 1];
          int modResult = firstTerm % secondTerm;
          
          System.out.println("" + firstTerm + " % " + secondTerm + " = " + modResult);
          
          product *= modResult;
        }
        System.out.println();
        System.out.println("product of mod results = " + product);
    
      }
    }

  3. #3
    DanK is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    wow thnkx thats cool

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Java Beginner needs urgent help!
    By Polyy in forum New To Java
    Replies: 3
    Last Post: 11-24-2008, 03:11 AM
  2. Java Beginner needs help!!
    By Polyy in forum New To Java
    Replies: 4
    Last Post: 11-23-2008, 02:11 AM
  3. Basic Question from Networking Beginner
    By JDCAce in forum Networking
    Replies: 7
    Last Post: 10-10-2008, 08:29 PM
  4. Confused beginner with Rectangle type question
    By sigssoft in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:56 AM
  5. beginner to Java
    By notwist in forum New To Java
    Replies: 15
    Last Post: 04-18-2008, 09:41 AM

Posting Permissions

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