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

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.

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

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.