rss
Twitter Delicious Facebook Digg Stumbleupon Favorites

Friday, October 7, 2011

A program to find factorial of given number

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5 ! = 5  \times  4  \times  3  \times  2  \times  1 = 120  \
The value of 0! is 1, according to the convention for an empty product.
The factorial operation is encountered in many different areas of mathematics, notably in combinatorics, algebra and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars. The notation n! was introduced by Christian Kramp in 1808.
The definition of the factorial function can also be extended to non-integer arguments, while retaining its most important properties; this involves more advanced mathematics, notably techniques from mathematical analysis.
The factorial function is formally defined by
 n!=\prod_{k=1}^n k \!
or recursively defined by
 n! = \begin{cases}1 & \text{if } n = 0, \\(n-1)!\times n & \text{if } n > 0.\end{cases}
Both of the above definitions incorporate the instance
0! = 1, \
in the first case by the convention that the product of no numbers at all is 1. This is useful because:
  • There is exactly one permutation of zero objects (with nothing to permute, "everything" is left in place).
  • The recurrence relation (n + 1)! = n! × (n + 1), valid for n > 0, extends to n = 0.
  • It allows for the expression of many formulas, like the exponential function as a power series:
 e^x = \sum_{n = 0}^{\infty}\frac{x^n}{n!}
  • It makes many identities in combinatorics valid for all applicable sizes. The number of ways to choose 0 elements from the empty set is \tbinom{0}{0} = \tfrac{0!}{0!0!} = 1. More generally, the number of ways to choose (all) n elements among a set of n is \tbinom nn = \tfrac{n!}{n!0!} = 1.
The factorial function can also be defined for non-integer values using more advanced mathematics, detailed in the section below. This more generalized definition is used by advanced calculators and mathematical software such as Maple or Mathematica.

class Factors{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);             
          int result = 1;
          while(num>0){
                result = result * num;
                num--;
          }
          System.out.println("Factorial of Given no. is : "+result);
   }
}

0 comments:

Post a Comment

Powered by Blogger.