include <stdio.h>
#include <math.h>
/* Prototypes */
void realRoots(double, double, double);
void imaginaryRoots(double, double, double);
int main() {
/* Local variables */
int option;
double a, b, c, d;
printf(" Quadratic Equation ax^2+bx+c=0 \n");
printf("******************* *******************\n");
/* Print list */
printf("\n1) Real roots\n");
printf("2) Imaginary roots\n");
printf("3) Repeated roots\n");
printf("4) Exit\n");
scanf("%d", &option);
printf("************* *************************\n");
/* Get a, b, and c from user */
printf("\na = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
/* Calculate determinant */
d = (b * b) - (4 * a * c);
/* Use switch statement */
switch(option) {
/* If 1 was selected */
case 1:
realRoots(a, b, sqrt(d));
break;
/* If 2 was selected */
case 2:
imaginaryRoots(a, b, sqrt(-d));
break;
/* If 3 was selected */
case 3:
break;
/* If 4 was selected */
case 4:
break;
}
/* End program */
return 0;
}
/* Evaluate real roots */
void realRoots(double a, double b, double d) {
/* Calculate */
double firstRoot = (-b/(2 * a)) + (d/(2 * a));
double secondRoot = (-b/(2 * a)) - (d/(2 * a));
/* Print */
printf("\nFirst Real Root: \t%lf\n", firstRoot);
printf("\nSecond Real Root: \t%lf\n", secondRoot);
}
/* Evaluate imaginary roots */
void imaginaryRoots(double a, double b, double d) {
/* Calculate */
double x = 2 * a;
double first_term = (-b)/x;
double second_term = (d)/x;
/* Print */
if(second_term >= 0) {
printf("\nFirst Imaginary Root: \t%lf + %lfi\n", first_term, second_term);
printf("\nSecond Imaginary Root: \t%lf - %lfi\n", first_term, second_term);
}else {
second_term = -(second_term);
printf("\nFirst Imaginary Root: \t%lf + %lfi\n", first_term, second_term);
printf("\nSecond Imaginary Root: \t%lf - %lfi\n", first_term, second_term);
}
}
Chat with our AI personalities
Pls try the following program.
#include <stdio.h>
#include <math.h>
/* Prototypes */
void realRoots(double, double, double);
void imaginaryRoots(double, double, double);
int main() {
/* Local variables */
int option;
double a, b, c, d;
printf(" Quadratic Equation ax^2+bx+c=0 \n");
printf("******************* *******************\n");
/* Print list */
printf("\n1) Real roots\n");
printf("2) Imaginary roots\n");
printf("3) Repeated roots\n");
printf("4) Exit\n");
/* Get option from user */
scanf("%d", &option);
printf("************* *************************\n");
/* Get a, b, and c from user */
printf("\na = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
/* Calculate determinant */
d = (b * b) - (4 * a * c);
/* Use switch statement */
switch(option) {
/* If 1 was selected */
case 1:
realRoots(a, b, sqrt(d));
break;
/* If 2 was selected */
case 2:
imaginaryRoots(a, b, sqrt(-d));
break;
/* If 3 was selected */
case 3:
break;
/* If 4 was selected */
case 4:
break;
}
/* End program */
return 0;
}
/* Evaluate real roots */
void realRoots(double a, double b, double d) {
/* Calculate */
double firstRoot = (-b/(2 * a)) + (d/(2 * a));
double secondRoot = (-b/(2 * a)) - (d/(2 * a));
/* Print */
printf("\nFirst Real Root: \t%lf\n", firstRoot);
printf("\nSecond Real Root: \t%lf\n", secondRoot);
}
/* Evaluate imaginary roots */
void imaginaryRoots(double a, double b, double d) {
/* Calculate */
double x = 2 * a;
double first_term = (-b)/x;
double second_term = (d)/x;
/* Print */
if(second_term >= 0) {
printf("\nFirst Imaginary Root: \t%lf + %lfi\n", first_term, second_term);
printf("\nSecond Imaginary Root: \t%lf - %lfi\n", first_term, second_term);
}else {
second_term = -(second_term);
printf("\nFirst Imaginary Root: \t%lf + %lfi\n", first_term, second_term);
printf("\nSecond Imaginary Root: \t%lf - %lfi\n", first_term, second_term);
}
}
by Ford
(Uses Square Root Function) PRINT "Ax^2 + Bx + C = 0" INPUT "A = ", A INPUT "B = ", B INPUT "C = ", C D = B * B - 4 * A * C IF D > 0 THEN DS = SQR(D) PRINT "REAL ROOTS:", (-B - D) / (2 * A), (-B + D) / (2 * A) ELSE IF D = 0 THEN PRINT "DUPLICATE ROOT:", (-B) / (2 * A) ELSE DS = SQR(-D) PRINT "COMPLEX CONJUGATE ROOTS:", (-B / (2 * A)); "+/-"; DS / (2 * A); "i" END IF END IF
READ values of a, b and c,if a is zero then stop as we do not have a quadratic,calculate value of discriminantif D is zero then there is one root: ,if D is > 0 then there are two real roots: and ,if D is < 0 there are two complex roots: and ,PRINT solution.
/*Hello!! I'm aditya From Bangalore I have the solution of this program*/ #include <stdio.h> #include <math.h> main() { float a,b,c,x1,x2,delta=0; printf("enter the value of a,b,c\n"); scanf("f%f",&a,&b,&c); delta=((b*b)-(4ac)); if(a=0) { printf("the variables cannot form a quadratic equation\n"); } else { x1=(-b)+(sqrt(((b*b)-(4ac))/2a)) x2=(-b)-(sqrt(((b*b)-(4ac))/2a)) } if(delta=0) { printf("the roots are real and equal"); } if(delta>0) { printf("the roots are real and distinct"); } if(delta<0) { printf("the roots are imaginary"); x1=(-b)+(sqrt(((b*b)-((4ac))/(float)(2a)) x2=(-b)-(sqrt(((b*b)-((4ac))/(float)(2a)) } printf("the roots of the equation are %2.2f\n",x1,x2,delta); } /*the program is written by using simple-if and if-else constructs*/
#include <stdio.h> #include <conio.h> #include <math.h> void main() { float a, b, c; float root1, root2,de,d; printf("enter the values of a, b and c"); scanf("%f%f%f", &a,&b,&c); de=(b*b)-4*(a*c); d=sqrt(de); root1=(-b+d) /(2.0*a); root2=(-b-d) /(2.0*a); printf(" roots of the equation are %f %f", root1,root2); getch(); }
#include<stdio.h> #include<conio.h> void main() { float a,b,c,z,d,x,y; clrscr(); printf("Enter the value of a,b,c"); scanf("%f %f %f",&a,&b,&c); d=((b*b)-(4*a*c)); z=sqrt(d); x=(-b+z)/(2*a); y=(-b+z)/(2*a); printf("The Quadratic equation is x=%f and y=%f",x,y); getch(); } This answer does not think about imaginary roots. I am a beginner in C programming. There might be flaws in my code. But, it does well. the code is #include<stdio.h> #include<math.h> main() { float a, b, c; float dis, sqdis, real, imag, root1, root2; printf("This program calculates two roots of quadratic equation of the form ax2+bx+c=0.\n"); printf("\n"); printf(" please type the coefficients a, b and c\n"); printf("\n"); printf("a = "); scanf("%f", &a); printf("\n"); printf("b = "); scanf("%f", &b); printf("\n"); printf("c = "); scanf("%f", &c); printf("\n"); dis = (b*b-4*a*c); if(dis < 0) { sqdis = sqrt(-dis); real = -b/(2*a); imag = sqdis/(2*a); printf(" The roots of the quadratic equations are \n x1\t=\t %f + %f i\n x2\t=\t %f - %f i\n", real, imag, real, imag); } else { sqdis = sqrt(dis); root1 = -b/(2*a)+sqdis/(2*a); root2 = -b/(2*a)-sqdis/(2*a); printf("The two roots of the quadratic equations are %f and %f.\n", root1, root2); } system("pause"); }