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

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.

Which Language is Mostly Used in Programming?

  • C
  • C++
  • Java
  • .Net
See results without voting

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.

Comments 9 comments

Dayakar. T 6 years ago

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


Mohammed  5 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


faisalfazlani profile image

faisalfazlani 5 years ago

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


Kanakendu karmakar 5 years ago

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


chandan 5 years ago

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


suraj 5 years ago

why we need to print first and second root


jobin 3 years ago

i want recursion program as early as possible


arsh gill 2 years ago

thank you


khushi 22 months ago

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

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working