Java Reference
In-Depth Information
Chapter 24
Java Math for Engineers
Java Numerical Primitives
This chapter contains an assortment of numerical routines and primitives
that are often required in solving engineering problems. The chapter serves
a double purpose. The first one is to provide a small library of Java primi-
tives. Second, and more important, to demonstrate how you can use your
knowledge of the Java language to solve problems that are expressed in for-
mulas and equations.
Factorial
The factorial of a number is the product of all positive integers less than or
equal to the number, for example:
5!=5*4*3*2*1=120
A typical computer algorithms for the calculation of factorials is recur-
sive. The recursive definition of the factorial function is as follows:
0!=1
n!=n*(n-1)!forn>0
In Java code a recursive factorial function can be coded as follows:
int Factorial(int n)
{
if(n == 0)
return 1;
else
return n * Factorial(n - 1);
}
Search WWH ::




Custom Search