ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

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.

Which Language is Mostly Used in Programming?

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.

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Dayakar. T 6 years ago

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

    • profile image

      Mohammed  6 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 6 years ago

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

    • profile image

      Kanakendu karmakar 6 years ago

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

    • profile image

      chandan 5 years ago

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

    • profile image

      suraj 5 years ago

      why we need to print first and second root

    • profile image

      jobin 3 years ago

      i want recursion program as early as possible

    • profile image

      arsh gill 2 years ago

      thank you

    • profile image

      khushi 2 years ago

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

    Click to Rate This Article