Untitled
unknown
c_cpp
2 years ago
2.5 kB
5
Indexable
#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <stdio.h>
int main(void) {
int a, b, c;
float delta, x1, x2, absdelta, re1, re2, im1, im2; // creating a variables
printf("This program will solve the quadratic equation of the form: "); // intro
printf("ax^2+bx+c=0\n");
printf(" \n"); // just a gap
printf("Enter the value of a here: "); // asking for the value of a
scanf("%d", &a);
printf("Enter the value of b here: "); // asking for the value of b
scanf("%d", &b);
printf("Enter the value of c here: "); // asking for the value of c
scanf("%d", &c);
if (a == 0) {
if (b == 0) { // linear equation
if (c == 0) {
printf("\n");
printf("x can be any number");
}
else {
printf("\n");
printf("there is no solution");
}
}
else {
printf("\n");
printf("x = %f\n", -(float)c / b);
}
}
else {
printf("\n"); // just a gap
delta = pow((float)b, 2) - 4 * (float)a * (float)c; // computing the discriminant
printf("The discriminant is: %f\n", delta); // printing the discriminant value
printf("\n"); // just a gap
if (delta > 0) { // checking the value of discriminant (>0)
x1 = ((-(float)b + sqrt(delta)) / (2 * (float)a)); // computing the first root
x2 = ((-(float)b - sqrt(delta)) / (2 * (float)a)); // computing the second root
printf("The roots are: %f and %f\n", x1, x2); // printing the roots
}
if (delta == 0) { // checking the value of discriminant (=0)
x1 = ((-(float)b + sqrt(delta)) / (2 * (float)a)); // computing the root
printf("The root is: %f\n", x1); // printing the root
}
if (delta < 0) { // checking the value of discriminant (<0)
absdelta = delta * (-1); // computing the absolute value of diccriminant
re1 = (-(float)b / (2 * (float)a)); // computing the real parts of the roots
re2 = (-(float)b / (2 * (float)a));
im1 = (sqrt(absdelta) / (2 * (float)a)); // computing the imaginary parts of the roots
im2 = (-sqrt(absdelta) / (2 * (float)a));
printf("The roots are: %f + %fi and %f + %fi\n", re1, re2, im1, im2); // printing the roots
}
}
printf(" \n"); // just a gap
// printf("by queth"); // printing my nickname
return 0;
}Editor is loading...
Leave a Comment