martes, 15 de noviembre de 2016

Factorial C#

Hola, hoy vamos a ver el código para sacar el factorial de un numero en C#, espero les guste.


Código:

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

namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Numero=");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine("El factorial es "+factorial(n));
            Console.ReadKey();
        }
        public static int factorial(int n)
        {
                    int fact = 1;
                    if (n == 0 || n == 1)
                        return 1;
                    for (int i = n; i >= 1; i--)
                        fact *= i;
                    return fact;
        }
    }
}

Descargar
Abrir con Visual Studio.

No hay comentarios:

Publicar un comentario