Code C#: Giải phương trình bậc 2 - Console

Giải phương trình bậc 2 - Console


/*
Giải và biện luận phương trình bậc 2 (sử dụng ứng dụng Console)
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GiaiPT_bac2_console
{
    class Program
    {
        static void Main(string[] args)
        {
            float a, b, c;

            string xau;
            // Nhap a,b,c
            Console.WriteLine("\n Nhap he so: ");
            Console.Write("\n a= ");
            xau = Console.ReadLine();
            a = float.Parse(xau);
            Console.Write("\n b= ");
            xau = Console.ReadLine();
            b = float.Parse(xau);
            Console.Write("\n c= ");
            xau = Console.ReadLine();
            c = float.Parse(xau);

            // Giai va bien luan
            if (a == 0)
                if (b == 0)
                    if (c == 0)
                        Console.WriteLine("\n Phuong trinh vo so nghiem");
                    else
                        Console.WriteLine("\n Phuong trinh vo nghiem");
                else
                    Console.WriteLine("\n Phuong trinh co 1 nghiem x= {0}", c / b);
            else
            {
                
                float delta=b*b-4*a*c;
                if(delta<0)
                    Console.WriteLine("\n Phuong trinh vo nghiem");
                if(delta==0)
                    Console.WriteLine("\n Phuong trinh co nghiem kep x1=x2= {0}",-b/(2*a));
                if (delta > 0)
                {
                    float x1, x2;
                    x1 = (-b - (float)Math.Sqrt(delta)) / (2 * a);
                    x2 = (-b + (float)Math.Sqrt(delta)) / (2 * a);
                    Console.WriteLine("\n Phuong trinh co 2 nghiem x1= {0}; x2= {1}", x1, x2);
                }
            }
        }
    }
}
// Ấn CTRL + F5 để chạy.


Chúc các bạn thành công!

 

Tìm kiếm nội dung khác: