# Write a program to find roots of a quadratic equation using functions and switch statements.

Updated on March 15, 2010

## Question

Write a program to find roots of a quadratic equation using functions and switch statements.

## Code

```#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<iostream.h>
void main()
{
float a,b,c,x1,x2,disc;
clrscr();
printf("Enter the co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc>0)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("The roots are distinct\n");
}
if(disc==0)
{
x1=x2=-b/(2*a);
printf("The roots are equal\n");
printf("x1=%f\nx2=%f\n",x1,x2);
}
else
x1=-b/(2*a);
x2=sqrt(fabs(disc))/(2*a);
printf("The roots are complex\n");
printf("The first root=%f+i%f\n",x1,x2);
printf("The second root=%f-i%f\n",x1,x2);
getch();
}
```

## Output

Enter the co-efficients:26
45
56
The roots are complex
The first root=-0.865385+i1.185308
The second root=-0.865385-i1.185308.

See results

## Switch..Case

C++ Has a built-in multiple branch selection statement called switch. The switch statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statement associated with that constant are executed.

163

0

9

149

## Popular

5

1

• ### C Program Code for Addition of Two Polynomials using Arrays

22

0 of 8192 characters used

• khushi

3 years ago

i want a program to calculate the root of quadratic equation using function

• arsh gill

4 years ago

thank you

• jobin

5 years ago

i want recursion program as early as possible

• suraj

7 years ago

why we need to print first and second root

• chandan

7 years ago

its wrong.........@ 18 th statement it must be else if...

• Kanakendu karmakar

7 years ago

I want to create a project of official management by c programming, help me.

• faisalfazlani

7 years ago

dear i want a project of calendar in c programming....so plz help me .send me as sooon as possible

• Mohammed

8 years ago

Hi can you write program for this problem Please?

write a computer program to find the roots of the following equation

f(x) = -1 + 5.5x - 4x2 + 0.5x3

employing the following methods: Graphical, bisection, false position, newton, and secant.

Compute the ture error and compare grphically with the number of iterations for each methods.

Include in your report tilte page and the results

• Dayakar. T

8 years ago

thank you very much that was a very detailed and useful information

working