Ver Mensaje Individual
  #3 (permalink)  
Antiguo 04/02/2012, 06:48
elchache
 
Fecha de Ingreso: febrero-2012
Mensajes: 13
Antigüedad: 13 años
Puntos: 0
Respuesta: tengo codigos en c# que fallan y no entiendo

El proceso de suma iterativa me funciona bien pero el de reiterativa no, supongo que es la formula de resultado. Debe de hacer para por ejemplo dos valores 4*5=4+4+4+4+4


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SumaIterativaReiterativa
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int SumaIterativa(int a, int b)
{
int i, resultado;
resultado = 0;
for (i = 1; i <= a; i++)
{
resultado = resultado + b;
}
return resultado;
}
int SumaRecursiva(int a, int b)
{
int resultado;
if (b == 0)
{
resultado = a;
}
else
{
resultado = a+SumaRecursiva(a, b-1);
}
return resultado;

}

private void button1_Click(object sender, EventArgs e)
{
int a, b,resultado;
a = int.Parse(textBox1.Text);
b = int.Parse(textBox2.Text);
resultado = SumaIterativa(a, b);
MessageBox.Show("Suma Iterativa: "+resultado.ToString());
}

private void button2_Click(object sender, EventArgs e)
{
int a, b, resultado;
a = int.Parse(textBox1.Text);
b = int.Parse(textBox2.Text);
resultado = SumaRecursiva(a, b);
MessageBox.Show("Suma Recursiva: "+resultado.ToString());
}
}
}