Sample C Program To Find The Roots Of A Quadratic Equation.


#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{

float a, b, c, d, realp, imgp, r1, r2;
clrscr();

printf(" Enter the 3 numbers\n ");
scanf(" %f %f %f " ,&a, &b, &c);

if ( a == 0 || b == 0 || c == 0 )
    {
       printf(" Error input only non zero numbers\n ");
    }
else
   {
      d = b * b - 4 * a * c;
      if ( d == 0 )
        {
           printf(" Roots are equal\n ");
           r1 = r2 = - b / ( 2 * a );
           printf(" Root1 = %f, Root2 = %f ", r1, r2 );
        }
     else if(d>0)
        {
           printf( "Roots are real & distinct\n" );
           r1 = ( - b + sqrt ( fabs ( d ) ) ) / ( 2 * a );
           r2 = ( - b - sqrt ( fabs ( d ) ) ) / ( 2 * a );
           printf(" Root1 = %f, Root2 = %f", r1, r2);
         }
     else
        {
           printf(" Roots are imaginary\n ");
           realp = - b / ( 2 * a );
           imgp = sqrt ( fabs ( d ) ) / ( 2 * a );
           printf(" Root1 = %f + i%f, Root2 = %f - i%f ",realp, imgp, realp, imgp);
        }
    }

getch();

}



OUTPUT:

Enter the 3 numbers
1
4
2
Roots are real & distinct
Root1 = - 0.585786 , Root2 = - 3.414214



ALSO READ:

-- Program To Print First Ten(10) Natural Numbers.
-- Program To Print Fibonacci Series Upto 100.
-- Program To Check Whether A Number Is Prime Or Not.
-- Program To Compute The Reverse Of A Number.
-- Program To Print A Semicolon Without Using A Semicolon.

-- Program To Display Function Without Using The Main Function.
-- Program To Print Any Print Statement Without Using Semicolon.
-- Program To Display Its Own Source Code As Its Output.
-- Program To Swap Two Strings Using strcpy() Function.
-- Program to accept a string and print the reverse of the given string.

-- Program To Get IP Address.
-- Program To Accept An Integer & Reverse It.
-- Program To Convert Given Number Of Days To A Measure Of Time Given In Years, Weeks & Days.
-- Program To Illustrate The Concept Of Unions.
-- Program To Find The Size Of A Union.

... Back To C++ Programs Index

... Back To C++ FAQ's Index

... Back To C Programs Index

... Back To C FAQ's Index

... Back To HR Interview Index

Comments

Popular Posts