domingo, 13 de noviembre de 2011

recursividad consola

class Mate
{
public uint Factorial(uint n)
{
uint f = 1;
if (n == 0 || n == 1)
return f;
else
f = n * Factorial(n - 1);
return f;
}



//---------Opcion 1---------//
public void Invertir(int n)
{

Console.Write(n % 10);
if (n >= 10)
Invertir(n / 10);
//return n;
}

//--------Opcion 2---------//
public int []Invertir2(int[] num)
{
int[] resp = new int[num.Length];
for (int i = 0; i < num.Length; i++)
resp[i] = num[num.Length - i - 1];
return resp;

}

//public string Invertir_Cadena(string cadena)
//{
// string invertir = " ";
// for (int i = cadena.Length - 1; i >= 0; i--)
// {
// invertir = invertir + cadena.Substring(i, 1);

// }
// return invertir;

//}

public string invertir_Cadena(string s)
{
string cad = "";
for (int i = s.Length - 1; i >= 0; i--)
cad += s[i];
return cad;
}

public string InvertirCad(string s, int lenght)
{
if (lenght == 1)
return s;
else
return InvertirCad(s.Substring(1, s.Length - 1), lenght - 1) + s[0].ToString();
}

public int Fiboniacci(int n)
{
int F;
if (n == 1 || n == 2)
return 1;
else
F = Fiboniacci(n - 1) + Fiboniacci(n - 2);
return F;

}

static public int Stifel(int n, int r)
{
if (n == r || n == 1 || r == 0)
return 1;
else
return Stifel(n - 1, r) + Stifel(n - 1, r - 1);
}

/*static public int Stifel2(int n, int r)
{
if (n == r || n == 1 || r == 0)
return 1;
else
return 1;
}*/

}
}

Main:


class Program
{
static void Main(string[] args)
{
Mate num = new Mate();
Mate cad = new Mate();
string a = "hola";
Mate fib = new Mate();




uint Fac = num.Factorial(4);
Console.WriteLine("\n Factorial de 4 es: " + Fac);

Console.WriteLine("\n Invertir numero 1234");
num.Invertir(1234);


int n = fib.Fiboniacci(6);
Console.WriteLine("\n Fiboniacci " + n);

int coe = Mate.Stifel(8, 2);
Console.WriteLine("\n Coeficiente Binomial de (8, 2): " + coe);

//-------


Console.WriteLine("\nFrase a invertir HOLA: " + cad.InvertirCad(a,4));




Console.ReadKey();

No hay comentarios:

Publicar un comentario