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,
The factorial operation is encountered in many 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. Fabian Stedman, in 1677, described factorials as applied to change ringing.
Some Examples
n! | |
---|---|
0 | 1 |
1 | 1 |
2 | 2 |
3 | 6 |
4 | 24 |
5 | 120 |
6 | 720 |
7 | 5040 |
8 | 40320 |
Program
import java.util.*;
public class Factorial {
public static void main(String[] args)
{
Factorial f=new Factorial();
System.out.println("enter number within range");
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
f.Fact(no); }
public void Fact(int no)
{
double fact=1;
for(int i=1;i<=no;i++)
{
fact =fact*i;
}
System.out.println("fact is "+ fact);
}
}
public class Factorial {
public static void main(String[] args)
{
Factorial f=new Factorial();
System.out.println("enter number within range");
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
f.Fact(no); }
public void Fact(int no)
{
double fact=1;
for(int i=1;i<=no;i++)
{
fact =fact*i;
}
System.out.println("fact is "+ fact);
}
}
Thanks!!
ReplyDelete