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!.
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
Post a Comment