Factorial de un Número : C# : Java : Perl : Python

by admin

Share

Para todo número natural n, el factorial de n, será:

Pudiendo tomar su forma recursiva como:

Nota: 0! = 1 y 1! = 1

Código Factorial C#( o C Sharp)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                System.Console.WriteLine("Por favor ingresar argumento númerico\nEjemplo:: Program ");
            }
            else
            {
                try
                {
                    long numero = long.Parse(args[0]);
                    System.Console.WriteLine("El Factorial de {0} es = {1}", numero, Fact(numero));
                }
                catch (System.FormatException)
                {
                    System.Console.WriteLine("Por favor ingresar argumento númerico\nEjemplo:: Program ");
                }

            }
        }

        public static long Fact(long n)
        {
            if (n == 0)
            {
                return 1;
            }
            else
            {
                return n * Fact(n - 1);
            }

        }
    }
}

Factorial en Java

public class Factorial
{
        public static void main(String args[])
        {
                if( args.length == 0 )
                {
                        System.out.println("Por favor ingresar argumento numerico\nEjemplo: Factorial ");

                }else{
                        long numero = Long.parseLong(args[0]);
                        System.out.println("El Factorial de " + numero +" es = " + Fact(numero));
                }
        }

        public static long Fact(long n)
        {
            if (n == 0)
            {
                return 1;
            }
            else
            {
                return n * Fact(n - 1);
            }

        }
}

Factorial Python

def fact(n):
    if n == 0:
        return 1
    else:
        return n * fact(n-1)

Leave a Comment