Quadratic Equation and it’s solution to find roots using c programming language

In this article I will explain the Quadratic Equation and how to find out the roots of Quadratic equation using the c or c++ programming language program.
In this code to solve quadratic equation using simple program with explanation of each step in program using necessary comments.
Quadratic equation is just second degree polynomial. Quadratic equation has two value of it’s root means that the unknown variable have two value. Quadratic equation roots can find in two way, first is factoring the quadratic equation and second is using quadratic equation formula
Factoring method not suitable all time because of the in factoring method some quadratic equation too messy and doesn’t factor.
So the most preferable method to solve quadratic equation is quadratic equation formula.
Quadratic equation formula is as shown in figure.

quadratic equation formula

quadratic equation formula

In this formula square root part is decide the solution of quadratic equation, means that the if the (xCofficient)2-4*xSqureCofficient*constant is equal to zero this means that the roots of quadratic equation is real and both are equal and root can calculate using -xCofficient/(2.0*xSqureCofficient)
and if discriminant is greater then the zero this means that the roots of quadratic equation is real and both are distinct and both root can calculate using first root is ((-xCofficient+sqrt((xCofficient*xCofficient)-(4.0*xSqureCofficient*constant)))

/(2.0*xSqureCofficient))
second root is

((-xCofficient-sqrt((xCofficient*xCofficient)-(4.0*xSqureCofficient*constant)))

/(2.0*xSqureCofficient))

Code

/*
Programed by:- bharat kumar dhaker
Website-www.computersciencearticle.in
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>    
//math.h for calcualte the squre root of xCofficient
void main()
{
clrscr();
double xFirst, xSecond, Discriminant;
int xSqureCofficient, xCofficient,constant;
printf("enter the value of Quadratic 
Equation xSqureCofficient, xCofficient,constantn");
scanf("%d",&xSqureCofficient);
scanf("%d",&xCofficient);
scanf("%d",&constant);
/* next line calculate the discriminant of quadratic equation
square of xCofficient-4*xSqureCofficient*constant
*/
Discriminant=(xCofficient*xCofficient)-(4*xSqureCofficient*constant);

/* If the discriminant is equal 
to zero this means that the roots of quadratic 
equation is real and both are equal
*/
if(Discriminant==0)
{
printf("Quadratic Equation roots are real and both equal, 
which is asn");
xFirst=xSecond=-xCofficient/(2.0*xSqureCofficient);
printf("First root=%f , Second root=%f",xFirst,xSecond);
}

/* If the discriminant is greater then the zero this means 
that the roots of quadratic equation is real and
both are distinct
*/
else if(Discriminant>0)
{
xFirst=

((-xCofficient sqrt((xCofficient*xCofficient)-
(4.0*xSqureCofficient*constant)))

/(2.0*xSqureCofficient));
xSecond=((-xCofficient-sqrt((xCofficient*xCofficient)-
(4.0*xSqureCofficient*constant)))

/(2.0*xSqureCofficient));
printf("Quadratic Equation roots are real and unequal, which is asn");
printf("First root= %f , Second root=%f",xFirst,xSecond);
}
/* If the discriminant is less then 
zero or negative that is roots of quadratic equation is imaginary
*/
else
{
printf("Quadratic Equation roots are imaginary");
}
getch();
}

Example:-
Lets take an example of quadratic equation
3x2 – 5x – 6 = 0
In this quadratic equation find out each coefficient
xSqureCofficient=3
xCofficient= -5
Constant= -6
enter these values in program above then the following output is generated

quadratic equation

quadratic equation solution in c output

that is roots of given quadratic equation is
First root     = 2.474810
Second root= -0.808143

One Response
  1. June 13, 2012