Last active
December 16, 2015 18:39
-
-
Save kodamirmo/5478915 to your computer and use it in GitHub Desktop.
Calcula facorial C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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