answersLogoWhite

0

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);

}

}

User Avatar

Wiki User

13y ago

Still curious? Ask our experts.

Chat with our AI personalities

JudyJudy
Simplicity is my specialty.
Chat with Judy
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
BeauBeau
You're doing better than you think!
Chat with Beau
More answers

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

User Avatar

Wiki User

13y ago
User Avatar

gadida goodu

User Avatar

Wiki User

12y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Program to find roots of quadratic equation using switch?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a Qbasic program to find the root of a quadratic equation using the formula if the result is less than 0 the program should print the root are immerginary and end the program?

(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 &gt; 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


Draw an algorithm amd flowchat to claculate the roots of quadratic equation Ax2 Bx C0?

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.


Write a c program to find the roots of a quadratic equation using if else?

/*Hello!! I'm aditya From Bangalore I have the solution of this program*/ #include &lt;stdio.h&gt; #include &lt;math.h&gt; main() { float a,b,c,x1,x2,delta=0; printf("enter the value of a,b,c\n"); scanf("f%f",&amp;a,&amp;b,&amp;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&gt;0) { printf("the roots are real and distinct"); } if(delta&lt;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*/


C program to calculate the roots of a quadratic equation using two pointer parameters?

#include &lt;stdio.h&gt; #include &lt;conio.h&gt; #include &lt;math.h&gt; void main() { float a, b, c; float root1, root2,de,d; printf("enter the values of a, b and c"); scanf("%f%f%f", &amp;a,&amp;b,&amp;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(); }


Write a program to solve a quadratic equation using the quadratic formula in c plus plus?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { float a,b,c,z,d,x,y; clrscr(); printf("Enter the value of a,b,c"); scanf("%f %f %f",&amp;a,&amp;b,&amp;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&lt;stdio.h&gt; #include&lt;math.h&gt; 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", &amp;a); printf("\n"); printf("b = "); scanf("%f", &amp;b); printf("\n"); printf("c = "); scanf("%f", &amp;c); printf("\n"); dis = (b*b-4*a*c); if(dis &lt; 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"); }