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)
![Trần Anh Quân [T1907A]](https://www.gravatar.com/avatar/7e3a8fe30cedd711f426fc59a9d48efc.jpg?s=80&d=mm&r=g)
Trần Anh Quân
2020-05-16 20:49:06
using System;
namespace ptb2
{
class Program
{
static void Main(string[] args)
{
float a, b, c;
double x1, x2, d;
Console.Write("Nhap lan luot a,b,c cho pt: ");
a = Int32.Parse(Console.ReadLine());
b = Int32.Parse(Console.ReadLine());
c = Int32.Parse(Console.ReadLine());
d = b * b - 4 * a * c;
if (a != 0)
{
if (d == 0)
{
x1 = -b / (2 * a);
Console.WriteLine("Pt co nghiem kep: x1 = x2 = {0}", x1);
}
else if (d > 0)
{
x1 = (-b + Math.Sqrt(d)) / (2 * a);
x2 = (-b - Math.Sqrt(d)) / (2 * a);
Console.Write("phuong trinh co 2 nghiem phan biet: x1 = {0}, x2 = {1}", x1, x2);
}
else
{
Console.WriteLine("PTVN");
}
}
else
{
if(b == 0)
{
Console.WriteLine("PTVN");
return;
}
else
{
x1 = -c / b;
Console.WriteLine("PT co 1 nghiem x ={0}", x1);
}
}
}
}
}
![nguyễn văn huy [T1907A]](https://www.gravatar.com/avatar/b107d14d7d43c142b68c12c377262371.jpg?s=80&d=mm&r=g)
nguyễn văn huy
2020-05-16 05:55:33
using System;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("nhập a:");
float a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("nhập b:");
float b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("nhập c:");
float c = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("tính denta:");
float denta = b * b - 4 * a * c;
if (a == 0)
{
Console.WriteLine("pt bac 1 có nghiệm x=" + (-c / b));
}
else(a != 0)
{
if (denta < 0)
{
Console.WriteLine("pt vô nghiệm");
}else if (denta == 0)
{
float x = -b / (2 * a);
Console.WriteLine("pt có nghiệm kep x1=x2=" + x);
}
else if (denta > 0)
{
double x1 = 0;
double x2 = 0;
x1 = ((-b / 2) - Math.Sqrt(denta) / a);
x2 = ((-b / 2) + Math.Sqrt(denta) / a);
Console.WriteLine("pt có hai nghiêm pb" + "x1=" + x1 + "và x2=" + x2);
}
}
}
}
}
![hoangduyminh [T1907A]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
hoangduyminh
2020-05-16 02:15:22
using System;
using System.Security.Cryptography.X509Certificates;
namespace PhuongTrinh
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Nhap vao a:");
float a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Nhap vao b:");
float b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Nhap vao c:");
float c = Convert.ToInt32(Console.ReadLine());
float denta = (b * 2) - (4* a * c);
Console.WriteLine("Phuong Trinh nhap vao la : \n {0}x2 + {1}x + {2} = 0 ", + a, b, c);
Console.WriteLine("Denta = {0}", +denta);
if(a == 0)
{
if( b == 0 && c!= 0)
{
Console.WriteLine("Phuong Trinh Vo Nghien\n");
}else if(b == 0 && c == 0)
{
Console.WriteLine("Phuong Trinh Co vo so nghiem\n");
}
else
{
float x = -c / b;
Console.WriteLine("Phuong Trinh Co Nghiem x = {0}", +x);
}
}
else
{
if (denta < 0)
{
Console.WriteLine("Phuong Trinh Vo Nghiem");
}
else if (denta == 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(denta) / a);
x2 = ((-b / 2) + Math.Sqrt(denta) / a);
Console.WriteLine("Phuong trinh co 2 nghiem phan biet:");
Console.WriteLine("x1 = {0}", +x1);
Console.WriteLine("x2 = {0}", +x2);
}
}
}
}
}
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-05-15 14:29:53
using System;
namespace Phuongtrinhbachai
{
public class ptbh
{
static void Main(String[] asgv)
{
double x1, x2, benta;
Console.Write("Nhap lan luot cac so a, b, c cho pt bac 2: ");
int a = Int32.Parse(Console.ReadLine());
int b = Int32.Parse(Console.ReadLine());
int c = Int32.Parse(Console.ReadLine());
if (a == 0)
{
if (b == 0)
{
Console.WriteLine("phuong trinh vo nghiem.");
return;
}
x1 = -c / b;
Console.Write("phuong trinh co 1 nghiem x = {0}", x1);
}
else
{
benta = b * b - 4 * a * c;
if (benta > 0)
{
x1 = (-b + Math.Sqrt(benta)) / (2 * a);
x2 = (-b - Math.Sqrt(benta)) / (2 * a);
Console.Write("phuong trinh co 2 nghiem phan biet: x1 = {0}, x2 = {1}",x1,x2 );
}
else if(benta == 0)
{
x1 = -b / 2 * a;
Console.Write("phuong trinh co nghiem kep x1 = x2 = {0}", x1);
}
else
{
Console.Write("phuong trinh vo nghiem.");
}
}
}
}
}
![Minh Nghia [T1907A]](https://www.gravatar.com/avatar/ecca255d725eed36a872105205af1b8e.jpg?s=80&d=mm&r=g)
Minh Nghia
2020-05-15 14:17:41
using System;
namespace PtBac2
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
double z, x1, x2;
Console.Write("Enter the value of a :");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the value of b :");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the value of c :");
c = Convert.ToInt32(Console.ReadLine());
z = b * b - 4 * a * c;
if(z == 0)
{
Console.Write("The equation has a distinct solution .\n");
x1 = -b / (2.0 * a);
x2 = x1;
Console.Write("A discriminant solution {0}\n ", x1);
}
else if(z > 0)
{
Console.WriteLine("The equation has tow distinct solution :\n");
x1 = (-b + Math.Sqrt(z)) / (2 * a);
x2 = (-b - Math.Sqrt(z)) / (2 * a);
Console.WriteLine("The equation has one solutions x1 = {0}\n ", x1);
Console.WriteLine("The equation has two solutions x2 = {0}\n ", x2);
}
else
{
Console.WriteLine("The equation has no solution.");
Console.ReadKey();
}
}
}
}
![Trần Mạnh Dũng [T1907A]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
Trần Mạnh Dũng
2020-05-15 12:40:05
using System;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace phuongtrinhbac2
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
bool vonglap = true;
while (vonglap)
{
Console.WriteLine("=====================");
Console.WriteLine("Nhập A: ");
float a = float.Parse(Console.ReadLine());
Console.WriteLine("Nhập B: ");
float b = float.Parse(Console.ReadLine());
Console.WriteLine("Nhập C: ");
float c = float.Parse(Console.ReadLine());
float d = b * b - a * c;
float e = -c / b;
double f = Math.Sqrt(-c / a);
if (a == 0)
{
if (b == 0)
{
Console.WriteLine("Phương trình không có nghĩa");
}
else
{
Console.WriteLine("Phương trình trở về phương trình bậc 1: {0}x + {1} = 0", b, c);
Console.WriteLine("x = " + e);
}
}
else
{
if(b == 0)
{
if(c < 0)
{
Console.WriteLine("Phương trình: {0}x^2 + {1} = 0 ", a, c);
Console.WriteLine("Phương trình có nghiệm là x = " + f);
}
else
{
Console.WriteLine("Phương trình: {0}x^2 + {1} = 0 ", a, c);
Console.WriteLine("=> Phương trình không có nghĩa");
}
}
else
{
Console.Write("Phương trình: {0}x^2 + {1}x + {2} = 0 ", a, b, c);
if (d < 0)
{
Console.WriteLine("Phương trình vô nghiệm");
}
else if (d == 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(d)) / a;
x2 = ((-b / 2) + Math.Sqrt(d)) / a;
Console.WriteLine("\nPhương trình có 2 nghiệm phân biệt: ");
Console.WriteLine("x1 = {0}", x1);
Console.WriteLine("x2 = {0}", x2);
}
}
}
}
}
}
}
![Phí Văn Long [T1907A]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
Phí Văn Long
2020-05-15 10:08:26
using System;
namespace BT2
{
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);
}
}
}
}
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-05-15 10:01:58
using System;
namespace Ptb2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
int a, b, c;
Console.WriteLine("a*x^2 + b*x + c = 0");
Console.WriteLine("nhap a : ");
a = int.Parse(Console.ReadLine());
Console.WriteLine("nhap b : ");
b = int.Parse(Console.ReadLine());
Console.WriteLine("nhap c : ");
c = int.Parse(Console.ReadLine());
Console.Clear();
ptb2(a, b, c);
}
static String showptb2(int a, int b, int c)
{
return "\n\n\n"+ "pt : " + a + "*x^2 + " + b + "*x + " + c + " = 0\n";
}
static void ptb2(int a, int b, int c)
{
float x;
string pt = showptb2(a, b, c);
if (a == 0)
{
Console.WriteLine("(a = 0) --> pt bac 1 ! ");
}
else
{
int denta = b * b - 4 * a * c;
if (denta < 0)
{
Console.WriteLine(pt + " vo nghiem ! ");
}
else if (denta == 0)
{
x = (-b) / (2 * a);
Console.WriteLine(pt + " co 1 nghiem x = {0}", x);
}
else
{
Console.WriteLine(pt + " co 2 nghiem phan biet : \n");
Console.WriteLine("x = {0} va x = {1}", (-b + Math.Sqrt(denta)) / (2 * a), (-b - Math.Sqrt(denta)) / (2 * a));
}
}
}
}
}