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 #
Giải phương trình bậc 2 : ax2 + bx + c = 0
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Nguyễn Hoàng Anh [C1907L]](https://www.gravatar.com/avatar/5b7bb435cae0d0a0fd414f6fdd0adc87.jpg?s=80&d=mm&r=g)
Nguyễn Hoàng Anh
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");
}
}
}
}
![trung [C1907L]](https://www.gravatar.com/avatar/67c23432e4710f33dd14e580d41b0379.jpg?s=80&d=mm&r=g)
trung
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);
}
}
}
![Ngô Quang Huy [C1907L]](https://www.gravatar.com/avatar/8e54dbf5994077ce599f49278164ae79.jpg?s=80&d=mm&r=g)
Ngô Quang Huy
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");
}
}
}
}
![Lê Minh Bắc [T1907A]](https://www.gravatar.com/avatar/22abcac77d8ca5e01144e240abb48d22.jpg?s=80&d=mm&r=g)
Lê Minh Bắc
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();
}
}
}
![lê văn phương [T1907A]](https://www.gravatar.com/avatar/a07ddfb51e1e7189c76b4e42dbdbcddc.jpg?s=80&d=mm&r=g)
lê văn phương
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();
}
}
}
![NguyenHuuThanh [T1907A]](https://www.gravatar.com/avatar/035e4f4fed661b8e1c3e066e43cd5e41.jpg?s=80&d=mm&r=g)
NguyenHuuThanh
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();
}
}
![thienphu [T1907A]](https://www.gravatar.com/avatar/c4573ea65e411176c1852fd8584f1ab1.jpg?s=80&d=mm&r=g)
thienphu
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();
}
}
}
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
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!");
}
}
}
}
![Trần Ngọc Hải [T1907A]](https://www.gravatar.com/avatar/9f7a27af52cbf0d52c32019e1354d60a.jpg?s=80&d=mm&r=g)
Trần Ngọc Hải
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);
}
}
}
}
![Thành Lâm [T1907A]](https://www.gravatar.com/avatar/fb1b94f4caad069ee6e3f42ea2221b49.jpg?s=80&d=mm&r=g)
Thành Lâm
2020-05-17 17:05:34
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));
}
}
}
}