0

I am learning Java and discovering that a little bit of knowledge is confusing. The goal is to write a method that is the equivalent of the n! function. I am using a for loop to multiply a variable declared outside the method. All I get back is 0.

What am I doing wrong?

//
// Complete the method to return the product of
// all the numbers 1 to the parameter n (inclusive)
// @ param n
// @ return n!

public class MathUtil
{
   public int total;

   public int product(int n)
  {
   for (int i = 1; i == n; i ++)
   {
       total = total * i;

    }
    return total;

  }
}
6
  • 1
    Read this line of code adn tell me what seems wrong with it: for (int i = 1; i == n; i ++) Hint: initialization, condition, incrementation. Commented Mar 11, 2016 at 3:38
  • Total is not initialized with a value. Commented Mar 11, 2016 at 3:39
  • @kirbyquerby Irrelevant Commented Mar 11, 2016 at 3:39
  • 3
    @Kon. No it's not, lmao. Commented Mar 11, 2016 at 3:40
  • 1
    Please learn some basic debugging skills. Learn to use a debugger, or at least print out information at some meaningful positions. You should be able to solve the problems easily yourself Commented Mar 11, 2016 at 3:50

4 Answers 4

4

There is actually a lot of problems in your code:

  • It does not make sense to make it an instance method.
  • You have not initialize your total to a reasonable value.
  • The condition in your for loop is wrong
  • Method is not given a meaningful name
  • Messy indentation
  • (The list keeps growing...)

So here is a slightly improved version

public class MathUtil
{
  //
  // Complete the method to return the product of
  // all the numbers 1 to the parameter n (inclusive)
  // @ param n
  // @ return n!

  public static int factorial(int n)
  {
    int total = 1;
    for (int i = 1; i <= n; i ++)
    {
      total = total * i;
    }
    return total;
  }
}

so that you can call it as MathUtil.product(123) instead of some weird new MathUtil().product(123)

Personally I would rather do something like

result = n;
while (--n > 0) {
    result *= n;
}
return result;
Sign up to request clarification or add additional context in comments.

3 Comments

Also, product is is a bad name in the first place. It doesn't appropriately describe the functionality. factorial would be a better name, since that's what is actually does.
@Bryan Yes! I was planning to mention that too! Dunno why I missed that at last. Thanks for reminding
That last bit is clever! I may use that! I never considered using the commutative property to do the multiplication in reverse. I've always done factorials by counting up. It might be helpful to make a small note on the difference between pre vs post increment though, since your compact solution hinges on it whereas the for loop does not.
2

You are missing the initialization. Now I added the default value to 1. And you also have to change the condition. The for loop has to go on as long as the var i is smaller or equal to n.

public class MathUtil
{
  public int total = 1;

  public int product(int n)
  {
    for (int i = 1; i <= n; i ++)
    {
     total = total * i;
    }
   return total;

  }
 }

1 Comment

I did correct it. Now it should be fine, because you go from one to N.
1

you didn't initialized total, therefore it's 0. Whenever you multiply 0 with anything, you will get 0 as result.

Comments

1
public int total = 1;

    public int product(int n) {
        for (int i = 1; i <= n; i++) {
            total = total * i;

        }
        return total;

    }

you havent initialized total. It defaults to 0. Then when you multiple anything with total, you get 0 as result

1 Comment

Please add some explanation rather than just solving it and posting code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.