Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!.
For example:
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
Here, 4! is pronounced as "4 factorial".
The factorial is normally used in Combinations and Permutations (mathematics).
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
Here, 4! is pronounced as "4 factorial".
The factorial is normally used in Combinations and Permutations (mathematics).
Program : (Using Recursion)
package net.jubiliation.example;
/**
* @author Gaurav
*
*/
public class FactorialExample {
* @author Gaurav
*
*/
public class FactorialExample {
static int factorialFunction(int number) {
if (number == 0)
return 1;
else
return (number * factorialFunction(number - 1));
}
public static void main(String args[]) {
int number = 5;// It is the number to calculate factorial
System.out.println("Factorial of " + number + " is: " + factorialFunction(number));
}
}
int number = 5;// It is the number to calculate factorial
System.out.println("Factorial of " + number + " is: " + factorialFunction(number));
}
}
Result :
Factorial of 5 is: 120
===========================================================
Program : (Using Iteration)
package net.jubiliation.example;
/**
* @author Gaurav
*
*/
public class FactorialExample {
* @author Gaurav
*
*/
public class FactorialExample {
static int factorialFunction(int number) {
int result = 1;
for (int i = 1; i <= number; i++) {
result = result * i;
}
return result;
}
public static void main(String args[]) {
int number = 5;// It is the number to calculate factorial
System.out.println("Factorial of " + number + " is: " + factorialFunction(number));
}
}
int number = 5;// It is the number to calculate factorial
System.out.println("Factorial of " + number + " is: " + factorialFunction(number));
}
}
Result :
Factorial of 5 is: 120
Comments
Post a Comment