Matematica discreta recurcividad
- 1. namespace mat_dis_recurcividad
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region funciones
public int ContarDigito(int n)
{
if (n < 10 && n > -10)
{
return 1;
}
else
{
return 1 + ContarDigito(n / 10);
}
}
public int ContarPares(int n)
{
if (n < 10 && n > -10)
{
if (n%2==0)
{
return 1;
}
else
{
return 0;
}
}
else
{
if ((n % 10) % 2 == 0)
{
return 1 + ContarPares(n / 10);
}
else
{
return ContarPares(n / 10);
}
}
}
public int SumaPares(int n)
{
if (n < 10 && n > -10)
{
if (n % 2 == 0)
{
return n;
}
else
{
return 0;
}
}
else
- 2. {
if ((n % 10) % 2 == 0)
{
return (n%10) + ContarPares(n / 10);
}
else
{
return ContarPares(n / 10);
}
}
}
// invertir
//eliminar pares
//contar digitos pares
#endregion
#region botones
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(
ContarDigito(int.Parse(textBox1.Text)).ToString());
}
private void button2_Click(object sender, EventArgs e)
{
int x=ContarPares (int.Parse(textBox1.Text));
if(x==0)
{
MessageBox.Show("NO HAY DIGITOS PARES");
}
else
{
MessageBox.Show("LA CANTIDAD DE DIGITOS PARES ES:
"+ContarPares (int.Parse(textBox1.Text)).ToString());
}
}
private void button3_Click(object sender, EventArgs e)
{
int x=SumaPares (int.Parse(textBox1.Text));
if(x==0)
{
MessageBox.Show("NO HAY DIGITOS PARES");
}
else
{
MessageBox.Show("LA CANTIDAD DE DIGITOS PARES ES:
"+SumaPares (int.Parse(textBox1.Text)).ToString());
}
}
#endregion
}