You are on page 1of 6

#include <stdio.

h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
clrscr();
int a, b, c;
float x1,x2,delta;

cout<<" Giai phuong trinh bac hai."<<endl;


cout<<" Cho biet cac he so cua PT: "<<endl;
cout<<" a= "; cin>>a;
cout<<" b= "; cin>>b;
cout<<" c= "; cin>>c;

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


if (delta<0) cout<<" Phuong trinh vo nghiem."<<endl;
else
if (delta == 0) cout<<" Phuong trinh co nghiem kep x1 = x2 = "<<-b/(2*a);
else
{
x1=(float)(-b-sqrt(delta))/(2*a);
x2=(float)(-b+sqrt(delta))/(2*a);
cout<<" Phuong trinh co hai nghiem phan biet:"<<endl;
cout<<" x1= "<<x1<<endl;
cout<<" x2= "<<x2;
}
getch();
}

-Nhập vào hệ số a , b , c
-Xét a:
+ a=0 thì x = -c/b;
+ a != 0 (khác 0) thì :
-Xét delta = b*-4*a*c;
+ Nếu delta <0 thì vô nghiệm;
+ Nếu delta =0 thì nghiệm x = -b/2*a;
+ Nếu delta >0 thì có 2 nghiệm x1= (-b+sqrt(delta))/2*a ;x2 = (-b-sqrt(delta))/2*a;

Tam dc

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void PTbachai(int a,int b,int c)
{
float delta,x1,x2;
delta=b*b-(4*a*c);
if(delta<0) printf("\n pt vo nghiem");
else
if(delta==0)
{
x=-b/2*a;
printf("\n pt co nghiem kep:x1=x2=%f",x);
}
else
{
x1=(-b+sqrt(delta))/2*a;
x2=(-b-sqrt(delta))/2*a;
printf("\n pt co hai nghiem fan biet:");
printf("\n x1=%f",x1);
printf("\n x2=%f",x2);

}
void main()
{
int a,b,c;
printf("\n nhap a,b,c:",%d,%d,%d);
PTbachai(a,b,c);
getch();
}

Thuat giai khac

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void PTbachai(int a,int b,int c)
{
float delta,x1,x2;
delta=b*b-(4*a*c);
if(delta<0) printf("\n pt vo nghiem");
else
if(delta==0)
{
x=-b/2*a;
printf("\n pt co nghiem kep:x1=x2=%f",x);
}
else
{
x1=(-b+sqrt(delta))/2*a;
x2=(-b-sqrt(delta))/2*a;
printf("\n pt co hai nghiem fan biet:");
printf("\n x1=%f",x1);
printf("\n x2=%f",x2);

}
void main()
{
int a,b,c;
printf("\n nhap a,b,c:",%d,%d,%d);
PTbachai(a,b,c);
getch();
}

Cach nay vip ne

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int a,b,c;
float x,x1,x2,x3,x4,delta;
printf("\n nhap a=");
scanf("%d",&a);
printf("\n nhap b=");
scanf("%d",&b);
printf("\n nhap c=");
scanf("%d",&c);
delta=(b*b)-(4*a*c);
x=x1=x2=(-b)/(2*a);
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
x3=-b/c;
x4=-b/a;
if(a==0)
{
if(b==0)
printf("\n pt vsn %f");
else
printf("\n pt co nghiem %f",x);
}
else
printf("\n pt co nghiem &4.2f",x4);
if(a!=0)
{
if(delta<0)
printf("\n pt vn");
else if(delta==0)
printf("\n pt co nghiem kep %4.2f",x);
else
printf("\n pt co 2 nghiem pb %4.2f,%4.2f",x1,x2);
}
getch();
}

itachisasuke
22-10-2008, 00:26
đoạn này sai : int a,b,c;
sửa lại : float a, b, c;
và %d sửa thành %f

Của mình đây, dễ hiểu và 100% đúng (test gần nửa tiếng)

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>

int a, b, c;
double del, x, x1, x2, delta;

void main()
{

printf("Day la chuong trinh giai pt bac 2 ax2 + bx + c = 0.\n");


printf("Nhap vao ba he so a,b,c cach nhau bang khoang trang (space): ");
scanf("%d%d%d", &a, &b, &c);

if (a == 0)
{if (b == 0)
{if (c == 0)
printf("Phuong trinh vo so nghiem.\n");
else
printf("Phuong trinh vo nghiem.\n");
}
else
x = -c/b;
printf("Nghiem cua phuong trinh la: x = %0.2f. \n", x);
}
else
{
delta = b*b - 4*(a*c);
del = sqrt (delta);
x = -b / (2*a);
x1 = ((-b - del) / 2*a);
x2 = ((b - del) / 2*a);
if (delta < 0)
printf("Phuong trinh vo nghiem.\n");
else
if (delta == 0)
printf("Phuong trinh co nghiem kep x = %0.2f.\n", x);
else
printf("Phuong trinh co 2 nghiem phan biet: x1 = %0.2f, x2 = %0.2f \n", x1, x2);
getch();
}
}

[=========> Bổ sung bài viết <=========]

#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
int a,b,c;
cout<<"nhap vao so a =";cin>>a;
cout<<"nhap vao so b =";cin>>b;
cout<<"nhap vao so c =";cin>>c;
if(a==0)
{
if(b!=0)
cout<<"phuong trinh co mot nghiem:"<<(-c/b)<<endl;
else
{
if(c==0)
cout<<"phuong trinh co vo so nghiem"<<endl;
else
cout<<"phuong trinh vo nghiem"<<endl;
}
}
else
{
int d=(b*b)-4*(a*c);
cout<<"denta="<<d<<endl;
if(d==0)
cout<<"phuong trinh co nghiem kep:"<<((-b)/(2*a))<<endl;
else
{
if(d>0)
{
cout<<"phuong trinh co hai nghiem fan biet:"<<endl;
cout<<"x1="<<(-b+sqrt(d))/(2*a)<<endl;
cout<<"x2="<<(-b-sqrt(d))/(2*a)<<endl;
}
else
{
cout<<"phuong trinh vo nghiem";
}
}
}
getch();
}

Em thấy mấy bác làm thiếu điều kiện


Ví dụ như a=0, b=0 thì sao
Em thữ post bài em lên thữ nhé

#include<iostream>
#include<math.h>
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
double a, b, c, delta, x,x1,x2;
cout<<"---------Giai PT bac 2--------\n";
cout<<"Nhap a =";
cin>>a;
cout<<"Nhap b =";
cin>>b;
cout<<"Nhap c =";
cin>>c;
if(a!=0)
{
delta=b*b-4*a*c;
if(delta>0)
{

x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
cout<<"Phuong trinh co 2 nghiem phan biet\n\n";
cout<<"Nghiem x1 = "<<x1;
cout<<"\n";
cout<<"Nghiem x2 = "<<x2;
}
else
if(delta<0)
{
cout<<"Phuong trinh vo nghiem!!!";
}
else

{
x=-b/(2*a);
cout<<"Phuong trinh co nghiem kep la: x = "<<x;
}

}
else
if(b!=0)
{
x=-c/b;
cout<<"Nghiem cua phuong trinh la: x = "<<x;
}
else
if(c!=0)
{
cout<<"Phuong trinh vo nghiem!!!!";
}
else
cout<<"Phuong trinh co vo so nghiem!";

_getch();
return 0;

You might also like