Tuesday, 3 July 2012

Real roots of a Quadratic Equation in C


Code -


//___________Real roots of quadratic equation...(Function Example)________

/* Quadratic equations has roots if and only if the Discriminent of a quadratic equation is greater than or equal to 0

D = b * b - 4 a * c

The formula given above is used to check whether real roots exist or not before calculation */


//____________Inlcude Header Files____________

#include<stdio.h>

#include<conio.h>

//_________Inlcuded mainly for getch() function_________

#include<math.h>

//Included mainly for the sqrt() function______

int check_roots(int a, int b, int c);


//__________Function prototype to check whether real roots exist or not_________

void roots(double discriminent, int a, int b, int c);

//_________Function prototype to find out the roots of quadratic equation____________

int main()

//_________Function from where the program execution starts_____________

{
int xsq_coefficient = 0, x_coefficient = 0, constant = 0, discriminent = 0;

printf("\n\n      _______Program will calculate the roots of a Quadratic Equation______");

printf("\n\n Quadratic Equation is in form of :- \n\n\t A (x * x) + B (x) + C = 0, where C is a constant"); //_______Message for User________

printf("\n\n\t Enter the value for A - ");

//__Prompting the user to enter the value for coefficient of X square i.e. x * x__

scanf("%d", &xsq_coefficient);

//__Reading the user input and storing it in variable xsq_coefficient______

printf("\n\t Enter the value for B - ");

//__Prompting the user to enter the value for coefficient of X ___

scanf("%d", &x_coefficient);

//_Reading the user input and storing it in variable x_coefficient__

printf("\n\t Enter the value for C - ");

//_Prompting the user to enter the value of C__

scanf("%d", &constant);

//_Reading the user input and storing it in variable constant____

discriminent = check_roots (xsq_coefficient, x_coefficient, constant);

//_____Calling the check_roots function to get the value for discriminent______

if (discriminent >= 0)

//___Checking if discriminent if +ive and greater than equal to 0, roost exist____
{
roots((double)discriminent, xsq_coefficient, x_coefficient, constant);

/*___Calling the roots function to find out and display the roots for the quadratic equation__*/
}
else
{
printf("\n\n\t\t ________Roots are not real________");
//__________If roots dont exist, message gets displayed_______________

}

printf("\n\n_______________________________________________________________________________");

getch();

return 0;
/*_Return 0 tells the compiler that this program has executed successfully without any error___*/

}

int check_roots(int a, int b, int c)
{
int discriminent = 0;

discriminent = b * b - 4 * a * c;

//_Calculating discriminent using the given formula_

return discriminent;
}

void roots(double discriminent, int a, int b, int c)
{
double x1 = 0, x2 = 0;

//__Finding the first root_____

x1 = x1 + sqrt(discriminent);

x1 = x1 - b;

x1 = x1 / (2 * a);

//___Finding the second root___

if (discriminent == 0.00)
{
x2 = x1; //___If discriminent is 0, then roots are equal_
}
else
{
x2= x2 - sqrt(discriminent);

x2 = x2 - b;

x2 = x2 / (2 * a);

}

printf("\n\n  Roots for the Quadratic Equation :- %lf, %lf",x1,x2);

//____________Displaying the value for the roots______________

}

No comments:

Post a Comment