Hey guys,
I have this HW for school for Java but I just can't seem to figure it out. I know I have to use a for loop for it but I just can't seem to figure out everything else. Thanks :D

public int factorial(int n)
{
}

Recommended Answers

All 11 Replies

use a for loop, and have an answer variable that is set to 1. then decrement the for loop, and then multiply the number in the for loop to the number in the answer, and once you are done return the answer.

Hey guys,

I just did this but can you guys please explain why its this?

public class bob
{
	public static void main(String args[]) {
		int n=7;
		int fact=1;
		for(int i=1; i<=n; i++)
		{
			fact=fact*i;
		}
		System.out.println (fact);
    
}
}

no. instead of adding you want to subtract. and the n is the factorial (in your case 7, so 7 x 6 x 5 x 4 x 3 x 2 x 1). so you want to use n in your for loop. and you want to decrement to follow the pattern. then your fact value if your answer, so the operation would look like this

fact = fact * n;

explaining why a factor behaves in a certain way is more a question to ask a mathematician.

no. instead of adding you want to subtract. and the n is the factorial (in your case 7, so 7 x 6 x 5 x 4 x 3 x 2 x 1).

I don't understand why you have rejected the OP's solution just because it computes the answer in the order 1 x 2 x 3 x 4 x 5 x 6 x 7. The end result is that same.

@xiangzhuang: "I just did this but can you guys please explain why its this?"
That's a very strange question. If you just did that you must know why you did it that way. Is it possible that you copied that code form someone/somewhere else?

Member Avatar for hfx642

I thing the OP was asking why this (solution) works.
Whether you are looping from 1 to 7, or 7 down to 1, makes no difference.
You're just multiplying everything together.

I'm sorry I didn't notice that.. It doesn't make a difference because when you multiply the order doesn't matter. it is only when you are adding and multiplying in the same command. for example if you multiply 5 x 4 x 3 you get 60, and if you do (5 x 5) + 5 which gives an answer of 30, or if you do 5 * (5 + 5) which gives you an answer of 50. This is the reason BEDMAS was created.

Also the reason I rejected it was because factorials go from the highest number down. So I wanted to keep the factorial method the same as what a factorial is, but it doesn't actually matter.

you can also use recursion.

public int factorial(int n)
{
  if((n == 0) || (n == 1))
         return 1;
  else
         return n * factorial(n-1);
}
Member Avatar for hfx642

Yes, but I think that recursion is a bit too advanced for the stage the OP is at in Java.

for(int i=o;i<=n;i++)
{
fact=fact*i;
}
take two variables for one for factorial
and another for int
initialize them u will get the ans

@carlosreg: That's just a scruffy version of the code that xiangzhuang posted earlier in this thread, except that his version works and yours does not.
You can avoid much embarrassment by (a) reading the whole thread and (b) testing your code for elementary errors before posting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.