0

Hi, im new to this forum and also Java. Im currrently doing my degree 1st year. My lecturer gave me a question on factorial and im unable to solve it.

i need to explain why any number of threads can be executing inside of it without causing interference in the program below.


Public int factorial (int n)
{
If (n<0)
Return -1;
Else if (n ==0)
Return 1;
Int result =1;
For (i=1; i<=n;i++)
{
Int tem = result;
Temp= temp *I;
Result=temp
}
Return result;
}

Please help me to understand this and what should i do to complete it

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by spidey32
0

The simple answer to your threading question is that since no variables are declared as part of the object, but are local to the method, only the thread executing the method has access to them. For a more thorough explanation research the JVM and stack frames.

No one here will do your homework for you as far as fixing your factorial, but the hint is to start with the largest number in the factorial expression and work your way backwards.

Also, study the java style guide. Capital letters, by convention, are used to indicate classes, not variables or java keywords.

Edited by dononelson: n/a

0

Thank you bro. Im Working on the factorial code now. will get back here if i have ny problem.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.