Ai hộ mình chuyển từ pascal này sang C++ với Program Giai_PTB2; Uses crt; Var a,b,c, D: real; x1,x2: real; begin clrscr; write ('a,b,c: ' ); readln (a,b,c) ; D:= b*b - 4*a*c; x1:= (-b -sqrt (D))/(2*a); x2:= -b/a - x1; write (' x1 = ', x1:6:2,' x2 = ',x2:6:2); readln End.
2 câu trả lời
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a,b,c,delta;
float x, x1, x2;
cout << "Giai phuong trinh bac hai: ax^2+bx+c=0" << endl;
cout << "Nhap he so a = "; cin >> a;
if (a==0) {
cout << "Ban hay nhap lai! (So khac 0)" << endl;
cout << "Nhap he so a = "; cin >> a;
}
cout << "Nhap he so b = "; cin >> b;
cout << "Nhap he so c = "; cin >> c;
delta = b*b-4*a*c;
x = -b/(2*a);
x1 = (-b-sqrt(delta))/(2*a);
x2 = (-b+sqrt(delta))/(2*a);
if (delta<0) {
cout << "Phuong trinh vo nghiem!" << endl;
}
else if (delta==0) {
cout << "Phuong trinh co nghiem kep: x = " << x<< endl;
}
else {
cout << "Phuong trinh co 2 nghiem: \n x1 = " << x1 <<"\n x2 = " << x2 << endl;
}
return 0;
}
CHÚC BẠN HỌC TỐT!!!
#include <bits/stdc++.h>
using namespace std;
float a,b,c,D,x1,x2;
int main()
{
cin>>a>>b>>c;
if(a==0,b!=0)
x1=1.0*-c/b;
printf("%0.2f",x1);
if(a==0,b==0,c==0)
cout<<"Multiple";
if(a==0,b==0,c!=0)
cout<<"None";
if(a!=0)
D=b*b-4*a*c;
if(D>0)
x1=1.0*(-b-sqrt(D))/(2*a);
printf("%0.2f",x1);
x2=1.0*(-b+sqrt(D))/(2*a);
printf("%0.2f",x2);
if(D==0)
x1=1.0*-b/(2*a);
printf("%0.2f",x1);
if(D<0)
cout<<"None";
return 0;
}