programa para factorial de un numero

Factorial de un entero no negativo, es la multiplicación de todos los enteros positivos menores o iguales a n. Por ejemplo, el factorial de 6 es 6*5*4*3*2*1, que es 720. 

Un factorial está representado por un número y un ” ! marca ” al final. Es ampliamente utilizado en permutaciones y combinaciones para calcular los posibles resultados totales. Un matemático francés, Christian Kramp, utilizó por primera vez la exclamación. 
 

C++

// C++ program to find factorial of given number
#include <iostream>
using namespace std;
 
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
 
// Driver code
int main()
{
    int num = 5;
    cout << "Factorial of "
         << num << " is " << factorial(num) << endl;
    return 0;
}
 
// This code is contributed by Shivi_Aggarwal

C

// C program to find factorial of given number
#include <stdio.h>
 
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
 
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}

Java

// Java program to find factorial of given number
class Test {
    // method to find factorial of given number
    static int factorial(int n)
    {
        if (n == 0)
            return 1;
 
        return n * factorial(n - 1);
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println("Factorial of " + num
                           + " is " + factorial(5));
    }
}

Python3

# Python 3 program to find
# factorial of given number
 
# Function to find factorial of given number
def factorial(n):
      
    if n == 0:
        return 1
     
    return n * factorial(n-1)
  
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
  
# This code is contributed by Smitha Dinesh Semwal

C#

// C# program to find factorial
// of given number
using System;
 
class Test {
    // method to find factorial
    // of given number
    static int factorial(int n)
    {
        if (n == 0)
            return 1;
 
        return n * factorial(n - 1);
    }
 
    // Driver method
    public static void Main()
    {
        int num = 5;
        Console.WriteLine("Factorial of "
                          + num + " is " + factorial(5));
    }
}
 
// This code is contributed by vt_m

PHP

<?php
// PHP program to find factorial
// of given number
 
// function to find factorial
// of given number
function factorial($n)
{
    if ($n == 0)
        return 1;
    return $n * factorial($n - 1);
}
 
    // Driver Code
    $num = 5;
    echo "Factorial of ", $num, " is ", factorial($num);
 
// This code is contributed by m_kit
?>

Javascript

<script>
// Javascript to find factorial
// of given number
 
// function to find factorial
// of given number
function factorial(n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}
 
// Driver Code
let num = 5;
document.write("Factorial of " + num + " is " + factorial(num));
 
// This code is contributed by Saurabh Jaiswal
 
</script>

C++

// C++ program for factorial of a number
#include <iostream>
using namespace std;
 
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    int res = 1, i;
    for (i = 2; i <= n; i++)
        res *= i;
    return res;
}
 
// Driver code
int main()
{
    int num = 5;
    cout << "Factorial of "
         << num << " is "
         << factorial(num) << endl;
    return 0;
}
 
// This code is contributed by Shivi_Aggarwal

C

#include <stdio.h>
 
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    int res = 1, i;
    for (i = 2; i <= n; i++)
        res *= i;
    return res;
}
 
int main()
{
    int num = 5;
    printf(
        "Factorial of %d is %d", num, factorial(num));
    return 0;
}

Java

// Java program to find factorial of given number
class Test {
 
    // Method to find factorial of the given number
    static int factorial(int n)
    {
        int res = 1, i;
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}

Python3

# Python 3 program to find
# factorial of given number
 
# Function to find factorial of given number
def factorial(n):
      
    res = 1
     
    for i in range(2, n+1):
        res *= i
    return res
 
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
  
# This code is contributed by Smitha Dinesh Semwal

C#

// C# program to find
// factorial of given number
using System;
 
class Test {
    // Method to find factorial
    // of given number
    static int factorial(int n)
    {
        int res = 1, i;
 
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
 
    // Driver method
    public static void Main()
    {
        int num = 5;
        Console.WriteLine(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}
 
// This code is contributed by vt_m

PHP

<?php
 
// function to find factorial
// of given number
function factorial( $n)
{
    $res = 1; $i;
    for ($i = 2; $i <= $n; $i++)
        $res *= $i;
    return $res;
}
 
// Driver Code
$num = 5;
echo "Factorial of ", $num, " is ",
                    factorial($num);
 
// This code is contributed
// by anuj_67.
?>

Javascript

<script>
// JavaScript program to find factorial of given number
 
    // Method to find factorial of the given number
    function factorial(n)
    {
        var res = 1, i;
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
 
    // Driver method
     
        var num = 5;
        document.write("Factorial of " + num + " is " + factorial(5));
 
 
// This code is contributed by shivanisinghss2110.
 
</script>

C

// C program for factorial of a number
#include <stdio.h>
 
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
     if(n == 0)
          return 1;
    int i = n, fact = 1;
    while (n / i != n) {
        fact = fact * i;
        i--;
    }
    return fact;
}
 
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}

C++

// C++ program for factorial of a number
#include <iostream>
using namespace std;
 
// function to find factorial of given
// number using while loop
unsigned int factorial(unsigned int n)
{
    if(n == 0)
          return 1;
    int i = n, fact = 1;
    while (n / i != n) {
        fact = fact * i;
        i--;
    }
    return fact;
}
 
// Driver code
int main()
{
    int num = 5;
    cout << "Factorial of "
         << num << " is "
         << factorial(num) << endl;
    return 0;
}
// This code is contributed by Shivi_Aggarwal

Java

// Java program to find factorial of given number
 
class Test {
 
    // Method to find factorial of the given number
    static int factorial(int n)
    {
        if(n == 0)
           return 1;
        int i = n, fact = 1;
        while (n / i != n) {
            fact = fact * i;
            i--;
        }
        return fact;
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}

Python3

# Python 3 program to find
# factorial of given number
 
# Function to find factorial of given number
def factorial(n):
    if(n == 0):
       return 1
    i = n
    fact = 1
     
    while(n / i != n):
        fact = fact * i
        i -= 1
         
    return fact
 
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
  
# This code is contributed by Smitha Dinesh Semwal

C#

// C# program to find
// factorial of given number
using System;
 
class Test {
    // Method to find factorial
    // of given number
    static int factorial(int n)
    {
        if(n == 0)
            return 1;
        int i = n, fact = 1;
        while (n / i != n) {
            fact = fact * i;
            i--;
        }
        return fact;
    }
 
    // Driver method
    public static void Main()
    {
        int num = 5;
        Console.WriteLine(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}

Javascript

<script>
        // JavaScript Program to implement
        // the above approach
        // function to find factorial of given
        // number using while loop
        function factorial(n) {
            if (n == 0)
                return 1;
            let i = n, fact = 1;
            while (Math.floor(n / i) != n) {
                fact = fact * i;
                i--;
            }
            return fact;
        }
 
        // Driver code
        let num = 5;
        document.write("Factorial of "
            + num + " is "
            + factorial(num) + "<br>");
 
// This code is contributed by Potta Lokesh
 
    </script>

C++

// C++ program to find factorial of given number
#include <iostream>
using namespace std;
 
int factorial(int n)
{
    // single line to find factorial
    return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
 
// Driver Code
int main()
{
    int num = 5;
    cout << "Factorial of " << num << " is "<< factorial(num);
    return 0;
}
 
// This code is contributed by shivanisinghss2110

C

// C++ program to find factorial of given number
#include <stdio.h>
 
int factorial(int n)
{
    // single line to find factorial
    return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
 
// Driver Code
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}
 
// This code is contributed by  Rithika palaniswamy.

Java

// Java program to find factorial
// of given number
class Factorial {
 
    int factorial(int n)
    {
 
        // single line to find factorial
        return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        Factorial obj = new Factorial();
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + obj.factorial(num));
    }
}
 
// This code is contributed by Anshika Goyal.

Python3

# Python 3 program to find
# factorial of given number
 
def factorial(n):
 
    # single line to find factorial
    return 1 if (n == 1 or n == 0) else n * factorial(n - 1)
 
 
# Driver Code
num = 5
print ("Factorial of", num, "is",
      factorial(num))
 
# This code is contributed
# by Smitha Dinesh Semwal.

C#

// C# program to find factorial
// of the given number
using System;
 
class Factorial {
 
    int factorial(int n)
    {
 
        // single line to find factorial
        return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
    }
 
    // Driver Code
    public static void Main()
    {
        Factorial obj = new Factorial();
        int num = 5;
 
        Console.WriteLine(
            "Factorial of " + num
            + " is " + obj.factorial(num));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find factorial
// of given number
 
function factorial( $n)
{
     
    // single line to find factorial
    return ($n == 1 || $n == 0) ? 1:
             $n * factorial($n - 1);
}
 
    // Driver Code
    $num = 5;
    echo "Factorial of ", $num, " is ", factorial($num);
 
// This code is contributed by anuj_67.
?>

Javascript

<script>
 
// JavaScript program to find factorial of given number
function factorial(n)
{
    // single line to find factorial
    return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
 
// Driver Code
 
    var num = 5;
    document.write("Factorial of " +num +" is " + factorial(num));
 
// This code is contributed by shivanisinghss2110.
 
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *