By GokiSoft.com| 19:30 24/06/2020|
C Sharp

Giải phương trình bậc 2 - Lập Trình C# - Mệnh đề điều kiên if else trong # BT1400

Giải phương trình bậc 2 : ax2 + bx + c = 0

Liên kết rút gọn:

https://gokisoft.com/1400

Bình luận

avatar
Nguyễn Hoàng Anh [C1907L]
2020-06-24 14:00:33



using System;

namespace Lession1
{
    class Program
    {
        static void Main(string[] args)
        {
            float a, b, c;
            Console.WriteLine("Nhap vao a : ");
            a = float.Parse(Console.ReadLine());
            Console.WriteLine("Nhap vao b : ");
            b = float.Parse(Console.ReadLine());
            Console.WriteLine("Nhap vao c : ");
            c = float.Parse(Console.ReadLine());
            float delta = b * b - 4 * a * c;
            if (delta > 0)
            {
                Console.WriteLine("Phuong trinh co hai nghiem : ");
                Console.WriteLine("X1 = {0}", ((-b - Math.Sqrt(delta)) / 2 * a));
                Console.WriteLine("X2 = {0}", ((-b + Math.Sqrt(delta)) / 2 * a));

            }
            else if (delta == 0)
            {
                Console.WriteLine("Phuong trinh co hai nghiem kep nghiem");
                Console.WriteLine("X1 = X2 {0}", -b / 2 * a);
            }
            else if (delta < 0)
            {
                Console.WriteLine("Phuong trinh vo nghiem");
            }
        }
    }
}


avatar
trung [C1907L]
2020-06-24 13:31:32



using System;

class MainClass {
  public static void Main (string[] args) {
    float a,b,c;
    Console.WriteLine("Nhap vao so a:");
    a = float.Parse(Console.ReadLine());
    Console.WriteLine("Nhap vao so b:");
    b = float.Parse(Console.ReadLine());
    Console.WriteLine("Nhap vao so c:");
    c = float.Parse(Console.ReadLine());
    if (a==0 && b == 0 && c != 0)
    {
      Console.WriteLine("Phuong trinh vo nghiem");
      return;
    }

    if (a==0 && b == 0 && c == 0)
    {
      Console.WriteLine("Phuong trinh co nghiem la R");
      return;
    }

    if (a == 0 && b != 0) {
      Console.WriteLine("Phuong trinh co nghiem duy nhat la {0}",(-c/b));
      return;
    }

    float delta = b*b-4*a*c;
    if (delta < 0)
    {
      Console.WriteLine("Phuong trinh vo nghiem");
    }
    else if (delta == 0)
    {
      Console.WriteLine("Phuong trinh co nghiem duy nhat la {0}",-b/2*a);
    }
    else
    {
      Console.WriteLine("Phuong trinh co 2 nghiem la {0} va {1}",(-b+(Math.Sqrt(delta)))/4*a,(-b-(Math.Sqrt(delta)))/4*a);
    }
  }
}


avatar
Ngô Quang Huy [C1907L]
2020-06-24 13:22:05



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

namespace bai2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            a = int.Parse(Console.ReadLine());
            b = int.Parse(Console.ReadLine());
            c = int.Parse(Console.ReadLine());
            float delta = b*b-4*a*c;
            if(delta > 0)
            {
                double x1 = ((-1) * b + Math.Sqrt(delta)) / (2 * a);
                double x2 = ((-1) * b - Math.Sqrt(delta)) / (2 * a);
                Console.WriteLine("Nghiem x1 = {0}, x2 = {1}",x1,x2);
            }else if(delta==0){
                double x1 = (-1 * b) / (2 * a);
                Console.WriteLine("Nghiem kep x1 = {0}", x1);
            }else
            {
                Console.WriteLine("Vo Nghiem");
            }
            
        }
    }
}


avatar
Lê Minh Bắc [T1907A]
2020-05-19 04:50:04



using System;

namespace Lession2
{
    class PTB2
    {
        static void Main(string[] agrs)
        {
            Console.Write("Nhap  a = ");
            float a = float.Parse(Console.ReadLine());
            Console.Write("Nhap  b = ");
            float b = float.Parse(Console.ReadLine());
            Console.Write("Nhap  c = ");
            float c = float.Parse(Console.ReadLine());

            Console.WriteLine("{0}x^2 + {1}x + {2} = 0",a,b,c);

            if (a == 0)
            {
                if (b == 0)
                {
                    if (c == 0)
                    {
                        Console.WriteLine("Phuong trinh vo so nghiem");
                    }
                    else
                    {
                        Console.WriteLine("Phuong trinh vo nghiem");
                    }
                }
                else
                {
                    Console.WriteLine("Phuong trinh co 1 nghiem: x = {0}", (-c / b));
                }
            }
            else
            {

                float delta = b * b - 4 * a * c;
                float x1;
                float x2;

                if (delta > 0)
                {
                    x1 = (float)((-b + Math.Sqrt(delta)) / (2 * a));
                    x2 = (float)((-b - Math.Sqrt(delta)) / (2 * a));
                    Console.WriteLine("Phuong trinh co 2 nghiem la: x1 = {0} và x2 = {1}", x1, x2);

                }
                else if (delta == 0)
                {
                    x1 = (-b / (2 * a));
                    Console.WriteLine("Phuong trinh co nghiem kep la: x1 = x2 = {0} ", x1);

                }
                else
                {
                    Console.WriteLine("Phuong trinh vo nghiem");
                }
            }
            Console.ReadKey();
        }      
    }
}


avatar
lê văn phương [T1907A]
2020-05-19 01:58:25



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

namespace PTbac2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("Nhập số A:");
            float a = float.Parse(Console.ReadLine());
            Console.WriteLine("Nhập số B:");
            float b = float.Parse(Console.ReadLine());
            Console.WriteLine("Nhập số C:");
            float c = float.Parse(Console.ReadLine());
            float delta = (b/2) * (b/2) - a*c;
            Console.WriteLine("Phương trình nhập vào là:\n {0}x^2 + {1}x + {2} = 0 ",a,b,c);
            Console.WriteLine("Delta Phẩy = {0}", delta);

            if(delta < 0)
            {
                Console.WriteLine("Phương trình vô nghiêm.");
            }
            else if(delta == 0)
            {
                float x = (-b / 2) /a;
                Console.WriteLine("Phương trình có nghiệm kép x1 = x2 = {0}",x);

            }
            else
            {
                double x1 = 0;
                double x2 = 0;
                x1 = ((-b / 2) - Math.Sqrt(delta)) / a;
                x2 = ((-b / 2) + Math.Sqrt(delta)) / a;

                Console.WriteLine("Phương trình có 2 nghiệm phân biệt:");
                Console.WriteLine("x1 ={0}", x1);
                Console.WriteLine("x2 ={0}", x2);
            }

            Console.ReadKey();

        }
    }
}


avatar
NguyenHuuThanh [T1907A]
2020-05-18 05:15:19



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

namespace Bai_Tap_C__sharp
{
    class Class1
    {
        Console.Write("\nNhap  a = ");
            double a = Convert.ToDouble(Console.ReadLine());
        Console.Write("\nNhap  b = ");
            double b = Convert.ToDouble(Console.ReadLine());
        Console.Write("Nhap  c = ");
            double c = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine(a+"x2 +"+b+"x +"+c +" = 0");

            if (a == 0)
            {
                if (b == 0)
                {
                    Console.WriteLine("Phuong trinh vo nghiem");
                }
                else
                {
                    Console.WriteLine("Phuong trinh co 1 nghiem: "
                            + "x = " + (-c / b));
                }
                return;
            }

            double delta = b * b - 4 * a * c;
double x1;
double x2;

            if (delta > 0)
            {
                x1 = (double) ((-b + Math.Sqrt(delta)) / (2 * a));
                x2 = (double) ((-b - Math.Sqrt(delta)) / (2 * a));
                Console.WriteLine("Phuong trinh co 2 nghiem la: "
                        + "x1 = " + x1 + " và x2 = " + x2);
            }
            else if (delta == 0)
            {
                x1 = (-b / (2 * a));
                Console.WriteLine("Phuong trinh co nghiem kep la: "
                        + "x1 = x2 = " + x1);
            }
            else
            {
                Console.WriteLine("Phương trình vô nghiệm!");
            }
            Console.ReadKey();



        
        
    }
}


avatar
thienphu [T1907A]
2020-05-18 04:35:09



using System;

namespace Lesson2
{
    class Program
    {
        static void Main(string[] args)
        {
           

            // giai pt bac2 ax2+bx+c =0

            Console.Write("\nNhap  a = ");
            double a = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nNhap  b = ");
            double b = Convert.ToDouble(Console.ReadLine());
            Console.Write("Nhap  c = ");
            double c = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine(a+"x2 +"+b+"x +"+c +" = 0");

            if (a == 0)
            {
                if (b == 0)
                {
                    Console.WriteLine("Phuong trinh vo nghiem");
                }
                else
                {
                    Console.WriteLine("Phuong trinh co 1 nghiem: "
                            + "x = " + (-c / b));
                }
                return;
            }

            double delta = b * b - 4 * a * c;
            double x1;
            double x2;

            if (delta > 0)
            {
                x1 = (double)((-b + Math.Sqrt(delta)) / (2 * a));
                x2 = (double)((-b - Math.Sqrt(delta)) / (2 * a));
                Console.WriteLine("Phuong trinh co 2 nghiem la: "
                        + "x1 = " + x1 + " và x2 = " + x2);
            }
            else if (delta == 0)
            {
                x1 = (-b / (2 * a));
                Console.WriteLine("Phuong trinh co nghiem kep la: "
                        + "x1 = x2 = " + x1);
            }
            else
            {
                Console.WriteLine("Phương trình vô nghiệm!");
            }
            Console.ReadKey();

        }

    }
}





avatar
Luong Dinh Dai [T1907A]
2020-05-18 04:20:36



using System;

namespace Lesson2
{
    class Program
    {
        static void Main(string[] args)
        {
           

            #region giai pt bac2 ax2+bx+c =0

            Console.Write("\nNhap he so bac 2, a = ");
            double a = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nNhap he so bac 1, b = ");
            double b = Convert.ToDouble(Console.ReadLine());
            Console.Write("Nhap hang so tu do, c = ");
            double c = Convert.ToDouble(Console.ReadLine());

            giaiPT(a, b, c);
            Console.ReadKey();
            #endregion
        }

        public static void giaiPT(double a, double b, double c)
        {
            if (a == 0)
            {
                if (b == 0)
                {
                    Console.WriteLine("Phương trình vô nghiệm!");
                }
                else
                {
                    Console.WriteLine("Phương trình có một nghiệm: "
                            + "x = " + (-c / b));
                }
                return;
            }
            // tính delta
            double delta = b * b - 4 * a * c;
            double x1;
            double x2;
            // tính nghiệm
            if (delta > 0)
            {
                x1 = (double)((-b + Math.Sqrt(delta)) / (2 * a));
                x2 = (double)((-b - Math.Sqrt(delta)) / (2 * a));
                Console.WriteLine("Phương trình có 2 nghiệm là: "
                        + "x1 = " + x1 + " và x2 = " + x2);
            }
            else if (delta == 0)
            {
                x1 = (-b / (2 * a));
                Console.WriteLine("Phương trình có nghiệm kép: "
                        + "x1 = x2 = " + x1);
            }
            else
            {
                Console.WriteLine("Phương trình vô nghiệm!");
            }
        }
    }

}


avatar
Trần Ngọc Hải [T1907A]
2020-05-17 17:58:27



using System;

namespace PTB2
{
    class Program
    {
        static void Main(string[] args)
        {
            float a, b, c, delta;
            Console.WriteLine("Nhap vao a : ");
            a = float.Parse(Console.ReadLine());
            Console.WriteLine("Nhap vao b : ");
            b = float.Parse(Console.ReadLine());
            Console.WriteLine("Nhap vao c : ");
            c = float.Parse(Console.ReadLine());

            delta = b * b - a * c;
            Console.Write("Phuong trinh vua nhap la : \n {0}x2 + {1}x + {2} = 0 ", a, b, c);
            Console.WriteLine("\n Delta  = {0}", delta);

            if (delta < 0)
            {
                Console.WriteLine("Chuong trinh vo nghiem");
            }
            else if (delta == 0)
            {
                float x = (-b / 2) / a;
                Console.WriteLine("phuong trinh co nghiem kep x1 = x2 = {0}", x);
            }
            else
            {
                double x1 = 0;
                double x2 = 0;

                x1 = ((-b / 2) - Math.Sqrt(delta)) / a;
                x2 = ((-b / 2) + Math.Sqrt(delta)) / a;

                Console.WriteLine("Phuong trinh co 2 nghiem phan biet : ");
                Console.WriteLine("x1 = {0}", x1);
                Console.WriteLine("x2 = {0}", x2);
            }
        }

    }
}


avatar
Thành Lâm [T1907A]
2020-05-17 17:05:34


Thanh Lam
using System;

public class Class1
{
	public Class1()
	{
		static void Main(string[] args)
        {
			Console.WriteLine("======================Phương Trình Bậc 2 =====================");
			Console.WriteLine("Ax2 + Bx + C = 0");
			Console.WriteLine("Nhập giá trị của A: ");
			int a = int.Parse(Console.ReadLine());

			Console.WriteLine("Nhap gia tri cua B");
			int b = int.Parse(Console.ReadLine());

			Console.WriteLine("Nhap gia Tri Cua C: ");
			int c = int.Parse(Console.ReadLine());

			if(a < 0)
            {
				Console.WriteLine("Phuong trinh vo nghiem");
            }else if(a = 0)
            {
				Console.WriteLine("Phuong trinh co nghiem kep x1 = x2 = " + (-b % 2 * a));
            }
            else
            {
				Console.WriteLine("Phuong trinh co 2 nghiem x1 = " + ((b * b - 4 * a * c) / 2 * a) + "x2 = " +((-b * b - 4 * a * c)/2*a));
            }



        }
	}
}