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)

{

}

xiangzhuang 0 Newbie Poster

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)

{

}

sirlink99 56 Practically a Master Poster

xiangzhuang 0 Newbie Poster

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);
}
}
```

sirlink99 56 Practically a Master Poster

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;`

stultuske 1,116 Posting Maven Featured Poster

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

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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?

hfx642

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

sirlink99 56 Practically a Master Poster

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.

Edited by sirlink99 because: * n/a *

sharat90 0 Newbie Poster

you can also use recursion.

```
public int factorial(int n)
{
if((n == 0) || (n == 1))
return 1;
else
return n * factorial(n-1);
}
```

Edited by sharat90 because: * n/a *

hfx642

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

carlosreg 0 Newbie Poster

{

fact=fact*i;

}

take two variables for one for factorial

and another for int

initialize them u will get the ans

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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.