Hola maestros, alguien me puede ayudar con el algoritmo para saber si un numero es capicua o no , en C# .
plz!
Gracias de antemano :rolleyes:
Aunque es algo sencillo. Ahi lo tienes:
Se entiende verdad ¿?
Agregar al formulario 1 textbox y un boton. en el evento click del boton agregar lo siguiente:
Citar
string num;
string aux;
aux = "";
Boolean bol;
bol = false;
num = this.textBox1.Text;
for (int i = 0; i < num.Length; i++)
{
aux = num.Substring(i, 1) + aux;
}
if (aux == num)
{
bol = true;
}
MessageBox.Show("EL numero es capicua: " + bol);
Siiiiiii, gracias Maestro!!!!! ;D
Muy buena.
Cita de: N0vat0 en 16 Mayo 2008, 04:49 AM
Siiiiiii, gracias Maestro!!!!! ;D
:xD :xD :xD :xD
Aquí os dejo otra idea... es más o menos lo mismo, pero iterando sólo la mitad de la cadena ;)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ElHacker
{
class Program
{
public Program()
{
Console.WriteLine("El numero {0}es capicúa.", (ComprobarCapicua(Console.ReadLine()) ? "" : "no "));
}
private bool ComprobarCapicua(string number)
{
for (int i = 0; i < number.Length / 2; i++)
if (number != number[number.Length - 1 - i])
return false;
return true;
}
static void Main(string[] args)
{
new Program();
}
}
}
ahi te dejo otra forma de hacer el Capicua... yo tmb soy novato.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Class1
{
public static void Main()
{
string cadena = "";
Console.Write("Ingrese Numero :");
int n = int.Parse(Console.ReadLine());
string x = Convert.ToString(n);
do
{
int r = n % 10;
cadena = string.Concat(cadena,r);
n = n / 10;
} while (n != 0);
if (cadena == x)
Console.Write("Es capicua ");
else
Console.Write("No es capicua ");
Console.Read();
}
}
}