
Даны действительные числа a, b, c. Найдите все решения квадратного уравнения ax2 + bx + c = 0.
Формат входных данных Даны три действительных числа, a не равно 0. Формат выходных данных Выведите два действительных числа, если уравнение имеет два корня, одно действительное число – при наличии одного корня. При отсутствии действительных корней ничего выводить не нужно. помогите пожалуйста решить на С++

Ответы на вопрос

void main(){
float a, b,c;
printf("Введите коэффициенты квадратного уравнения - a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
float D,x1,x2;
D=b*b-4*a*c;
if(D>0){
x1=(-b+sqrt(D))/(2*a);
x2=(-b-sqrt(D))/(2*a);
printf("Уравнение имеет 2 корня x1=%f, x2=%f",x1,x2);
}
if(D==0)
{
x1=-b/(2*a);
printf("Уравнение имеет 1 корень x=%f",x1);
}
getch();
}



Solving the Quadratic Equation in C++
To solve the quadratic equation ax^2 + bx + c = 0 in C++, you can use the quadratic formula to find the roots of the equation. The quadratic formula states that the solutions for ax^2 + bx + c = 0 are given by:
x = (-b ± √(b^2 - 4ac)) / (2a)
Here's a C++ program to solve the quadratic equation:
```cpp
#include
int main() { double a, b, c; std::cout << "Enter the coefficients a, b, and c: "; std::cin >> a >> b >> c;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) { double root1 = (-b + sqrt(discriminant)) / (2 * a); double root2 = (-b - sqrt(discriminant)) / (2 * a); std::cout << "The roots are real and different: " << root1 << " and " << root2 << std::endl; } else if (discriminant == 0) { double root = -b / (2 * a); std::cout << "The root is real and equal: " << root << std::endl; } else { std::cout << "The roots are complex and imaginary." << std::endl; }
return 0; } ```
In this program, we first input the coefficients a, b, and c from the user. Then, we calculate the discriminant (b^2 - 4ac) to determine the nature of the roots. If the discriminant is greater than 0, the roots are real and different. If the discriminant is equal to 0, the roots are real and equal. If the discriminant is less than 0, the roots are complex and imaginary.
This program covers all cases and provides the appropriate output based on the nature of the roots.
I hope this helps! If you have any further questions or need additional assistance, feel free to ask.


Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili