JavaScript -Fakultät
function factorial(n) {
if (n < 0) return;
if (n < 2) return 1;
return n * factorial(n - 1);
}
TC5550
function factorial(n) {
if (n < 0) return;
if (n < 2) return 1;
return n * factorial(n - 1);
}
// FACTORIAL
// 5! = 5 * 4 *3 * 2 *1 = 120
// With recursion
const fact = (n) => {
if (n == 0) {
return 1;
}
else {
return n * fact(n - 1);
}
}
console.log(fact(5))
// with forloop
// FACTORIAL
// 5! = 5 * 4 *3 * 2 *1 = 120
const number = 5;
let fact =1 ;
for(let i=number;i>=1;i--)
{
console.log(i)
fact = fact * i;
}
console.log("FACTORIAL is :: ",fact);
function factorial (n){
j = 1;
for(i=1;i<=n;i++){
j = j*i;
}
return j;
}
unsigned long long factorial(unsigned long long num){
if(num<=0)
return 1;
return num * factorial(num-1);
}
public static void factorial(int num){
if(num<=0)
return 1;
return num * factorial(num-1);
}
//Java program to find factorial of a numberimport java.util.Scanner;public class factorial{ public static void main(String[] args) { //scanner class declaration Scanner sc = new Scanner(System.in); //input from user System.out.print("Enter a number : "); int number = sc.nextInt(); if(number >= 0) { //declare a variable to store factorial int fac = 1; for(int i = number ; i >= 1 ; i--) fac = fac * i; //display the result System.out.println("Factorial of "+number+" is "+fac); //closing scanner class(not compulsory, but good practice) } else System.out.println("Value is not defined, please re-enter the value"); sc.close(); }}