Bhaskara formula in C # with VS 2017

0

Personal Talk! I'm using Visual Studio to learn C #, I created a button that when clicked should show the result of a Bhaskara formula. But everything that appears on the screen when testing comes down to "NaN"

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

namespace bhaskara
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        /*Fórmula de Bhaskara -- */
        int a = 2, b = 2, c = 4;
        double delta, xl, xll , raizq;

        delta = (b * b) * (-4) * a * c;

        xl = ((-b) + Math.Sqrt(delta)) / 2 * a;
        xll = ((-b) - Math.Sqrt(delta)) / 2 * a;

        MessageBox.Show("O resultado do x linha é :" + xl);
        MessageBox.Show("Já o resultado de x duas linha é:" + xll);
    }
}
}
    
asked by anonymous 21.07.2018 / 15:47

1 answer

4

I've reformulated your code with some fixes:

1. Check for delta < 0 ;

2. Formula correction of delta ;

Follow the code with the changes (reused what I had already written):

/*Fórmula de Bhaskara -- */
        int a = 2, b = 2, c = 4;
        double delta, xl, xll, raizq;

        delta = ((b * b) - (4 * a * c));

        if (delta >= 0)
        {
            MessageBox.Show("O resultado de X1 é :"+((-b) + Math.Sqrt(delta)) / 2 * a);
            MessageBox.Show("O resultado de X2 é: " +((-b) - Math.Sqrt(delta)) / 2 * a);
        }
        else {
            MessageBox.Show("Delta < 0");
        }
    
21.07.2018 / 16:06