Skip to content

Instantly share code, notes, and snippets.

@kodamirmo
Last active December 16, 2015 18:39
Show Gist options
  • Save kodamirmo/5478915 to your computer and use it in GitHub Desktop.
Save kodamirmo/5478915 to your computer and use it in GitHub Desktop.
Calcula facorial C#
using System;
namespace HolaMundo
{
class ClaseFactorial
{
private int numero;
public static void Main (string[] args)
{
ClaseFactorial objeto=new ClaseFactorial();
objeto.leer();
objeto.imprmir();
}
public ClaseFactorial ()
{
numero=0;
}
public void leer()
{
Console.WriteLine("Introduce un numero");
numero = Convert.ToInt32(Console.ReadLine());
}
public int calcularFacorial ()
{
int facorial=1;
try{
if(numero==0 || numero==1)
return 1;
for (int i=1; i<=numero; i++) {
facorial=facorial*i;
}
return facorial;
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
return 1;
}catch (NotFiniteNumberException e)
{
Console.WriteLine(e.Message);
return 1;
}
}
public void imprmir ()
{
Console.WriteLine("El factorial del numero " + numero + " es " + calcularFacorial());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment