Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Thursday, 3 August 2017

java program to find the factorial of a Number

  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,

{\displaystyle 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 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);
}
}

1 comment: