answersLogoWhite

0


Best Answer

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
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

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

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

gadida goodu

This answer is:
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"); }

Related questions

What are the roots of the quadratic equation below?

That depends on the equation.


What are quadratic equations with real roots?

If the discriminant of the quadratic equation is zero then it will have 2 equal roots. If the discriminant of the quadratic equation is greater than zero then it will have 2 different roots. If the discriminant of the quadratic equation is less than zero then it will have no roots.


Why do you use square roots to solve quadratic equations?

Because it's part of the quadratic equation formula in finding the roots of a quadratic equation.


Can the answer to a quadratic equation be a decimal?

Yes. You can calculate the two roots of a quadratic equation by using the quadratic formula, and because there are square roots on the quadratic formula, and if the radicand is not a perfect square, so the answer to that equation has decimal.


How many roots does a quadratic equation?

2 roots


How do you solve a quadratic equation graphically?

The roots of the quadratic equation are the x-intercepts of the curve.


How many roots does a quadratic equation in one variable have?

A quadratic equation has two roots. They may be similar or dissimilar. As the highest power of a quadratic equation is 2 , there are 2 roots. Similarly, in the cubic equation, the highest power is 3, so it has three equal or unequal roots. So the highest power of an equation is the answer to the no of roots of that particular equation.


How do you formulate quadratic equation that can be solve by extracting square roots?

By using the quadratic equation formula


What is the solving for the roots of quadratic equation?

It is finding the values of the variable that make the quadratic equation true.


What are the roots of a quadratic equation called?

Either "roots" or "solutions".


What is the difference between a radical equation and a quadratic equation?

radical equations have sq roots, cube roots etc. Quadratic equations have x2.


Write an algorithm to find the root of quadratic equation?

Write an algorithm to find the root of quadratic equation