Ayuda con un simple programa en c#

(1/1)

Amagekure:
 double x1, x2,bn;
            int a = Convert.ToInt16(textBox1.Text);
            int b = Convert.ToInt16(textBox2.Text);
            int c = Convert.ToInt16(textBox3.Text);

            bn = b * -1;
            x1 = (bn + Math.Sqrt(b^2 - 4 * a * c)) / (2 * a);
            x2 = (bn - Math.Sqrt(b^2 - 4 * a * c)) / (2 * a);

            textBox4.Text = x1.ToString();
            textBox5.Text = x2.ToString();

pues necesito que me digan que esta mal aqui, pues intento que me diga las raizes realies de una ecuacion de 2 grado   A,B,C son los 3 numeros que corresponden a la escuasion pero cuando le aplico la formula el resultado es un texto 'NeuN'  y no los resultados numericos que estoy buscando, alguien sabio que me diga que error estoy cometiendo en este programa

Shell Root:
Te sale NeuN, porque le estas sacando Sqrt a un valor negativo, por ejemplo.
Código
MsgBox(Math.Sqrt(0 - 4))

Intentadlo así:
Código
       Dim x1, x2, bn As Double
       Dim a, b, c As Integer
 
       a = 1
       b = 6
       c = 1
 
       bn = b * -1
 
       x1 = (bn + Math.Sqrt(b ^ 2 - 4 * a * c)) / (2 *a)
       x2 = (bn - Math.Sqrt(b ^ 2 - 4 * a * c)) / (2 * a)
 
       MsgBox(x1)
       MsgBox(x2)

raul338:
Seria mejor asi:
Código
       Dim x1, x2, bn As Double
       Dim a, b, c As Integer
 
       a = 1
       b = 6
       c = 1
 
       bn = b * -1
 
       Dim radicando As Double = Math.Pow(b, 2) - 4  * a * c
 
       If radicando = 0 Then
              x1 = (bn) / (2 *a)
              MsgBox(x1 & " y es raiz doble")
       ElseIf radicando > 0 Then
              x1 = (bn + Math.Sqrt(radicando)) / (2 *a)
              x2 = (bn - Math.Sqrt(radicando)) / (2 * a)
              MsgBox(x1)
              MsgBox(x2)
       Else
              Msgbox ("La ecuacion no tiene racies reales")
       End If
 

AckeR:
Hola!

Pues tiene razón Alex, te dice eso porque saldria una raiz negativa (imaginaria).

Yo lo haría asi  :D

Código
           int a, b, c;
           double raiz,x1,x2;
 
           a = int.Parse(textBox1.Text);
           b = int.Parse(textBox2.Text);
           c = int.Parse(textBox3.Text);
 
 
           if (a == 0)
               MessageBox.Show("Error La Variable A no puede ser 0");
           else
           {
               raiz = (b * b) - 4 * (a * c);
               if (raiz < 0)
               {
                   MessageBox.Show("Error, raiz negativa (imaginaria)");
               }
               else
               {
                   x1 = ((-1 * b) + Math.Sqrt(raiz)) / (2 * a);
                   x2 = ((-1 * b) - Math.Sqrt(raiz)) / (2 * a);
 
                   textBox4.Text = x1.ToString();
                   textBox5.Text = x2.ToString();

suerte!

Amagekure:
Buena por tu ayuda gracias a ustedes 3 termine mi programa gracias la mejor web Elhacker.net FORO

Navegación

[0] Índice de Mensajes