WAP in JAVA to calculate factorial

Q > So , basically what is factorial ?
Def : A factorial is a function that multiplies  number by every number. For example 4!= 4*3*2*1=24. The function is used, among other things, to find the number of ways “n” objects can be arranged.
In mathematics, there are n! ( Factorial ways to arrange N Objects ) in sequence.
Q > How to calculate ?
For example :
Consider :
  • 2! = 2 x 1 = 2
The possibility of 2! is two ways like {2,1}, { 1,2 }.
Same as like :
4! = 4 x 3 x 2 x 1 = 24.
  • 24 = the arrangement of 4! is {1,2,3,4}, {2,1,3,4}, {2,3,1,4}, {2,3,4,1}, {1,3,2,4}, etc.
Same as like 5! , 10! , N!.
Factorial Program In Java
class Factoral
{
public static void main(String arg[])
{
             int n=5,fact=1;
        for(int i=1;i<=n;i++)
     {
        fact=fact*i;
  }
        System.out.println("factoral="+fact);
}
}
output:
factorial =120

Comments

Popular posts from this blog

A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the corresponding grade.

Write Java program to allow the user to input his/her age. Then the program will show if the person is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.

A Computer Salesman gets commission on the following basis using Java