# Solve Quadratic Equation- Program Codes

Quadratic equation is most popular equation to solve many scientific problems. In mathematics a quadratic equation is   defined as a polynomial equation with degree 2 using single variable.  We can Solve Quadratic Equation using the standard formula.

It is written as

`ax^2 + bx + c= 0`

Such that a, b and c ∈ R and a ≠ 0.  The values of a, b and c are called coefficients of the terms with degree of 2, 1 and 0 respectively.  The quadratic equation always defined with single variable. The values of a, b and c that satisfy the quadratic equation are called the roots of the quadratic equation.  The quadratic equation will always be solvable by exactly two roots. The roots are real or imaginary.

The formula to calculate two roots of the quadratic equation is

`(y,z)= (-b (+/-) squareroot(b^2-4*a*c))/2* a`

(+/-) allows you to create two roots with this formula, one by addition and one by subtraction.

#### Type of roots of Quadratic Equation

Depending on the value of D= b^2-4*a*c the roots are

• Real and unequal if D>0
• Real and equal id D=0
• Imaginary and unequal if D<0

To calculate the roots we simply put the values of coefficients in the formula given and calculate two values using + and – between –b and D =(squareroot(b^2-4*a*c))

Here are the program codes to Solve Quadratic Equation.

#### Java Code

```import java.io.*;
import java.lang.Math;
import java.lang.String;
{
void CalRoot(int a, int b, int c )
{
double  root1=0.0, root2=0.0;
//initialise empty string for storing quadratic equation
String str="";
//calculate roots
root1= (int)(-b+Math.sqrt(b*b-4*a*c))/(2*a);
root2= (int)(-b-Math.sqrt(b*b-4*a*c))/(2*a);
//create the string of quadratic equation
str=String.valueOf(a)+"X^2";
if (b<0)
str=str+String.valueOf(b)+"X^1";
else
str=str+"+"+String.valueOf(b)+"X^1";
if (c<0)
str=str+String.valueOf(c)+"X^0";
else
str=str+"+"+String.valueOf(c)+"X^0";
//display equation and roots
System.out.println("Root 1="+(int)root1);
System.out.println("Root 2="+(int)root2);
}

public static void main(String args[])
{
//initialise variables
int a=1, b=3, c=-10;
// declare object
//call the object method
qe.CalRoot(a,b,c);
}
}```

#### C Code

```#include<stdio.h>
#include<stdlib.h>
#include<math.h>

main()
{
//Variable declaration
int a,b,c, root1, root2;
printf("\nEnter a the Coefficient of X^2  : ");
scanf("%d",&a);
printf("\nEnter b the Coefficient of X^1 : ");
scanf("%d",&b);
printf("\nEnter c the Coefficient of X^0 : ");
scanf("%d",&c);
//Calculate Roots
root1= (-b+sqrt(b*b-4*a*c))/(2*a);
root2= (-b-sqrt(b*b-4*a*c))/(2*a);
// Display quadratic equantion as string
if (b<0)
printf("%dX^1",b);
else
printf("+%dX^1",b);
if (c<0)
printf("%dX^0\n",c);
else
printf("+%dX^1\n",c);
// Print Calculated Roots
printf("Root 1=%d\n",root1);
printf("Root 2=%d\n",root2);
}```

#### Python Code

```import math
a=int(input("Enter a the Coefficient of X^2  : "))
b=int(input("Enter b the Coefficient of X^1 : "))
c=int(input("Enter c the Coefficient of X^0 : "))
try:
#calculate roots
root1= (-b+math.sqrt(b*b-4*a*c))/(2*a)
root2= (-b-math.sqrt(b*b-4*a*c))/(2*a)
qe=str(a)+"X^2"
if (b<0):
qe=qe+str(b)+"X^1"
else:
qe=qe+"+"+str(b)+"X^1"
if (c<0):
qe=qe+str(c)+"X^0"
else:
qe=qe+"+"+str(c)+"X^0"
#display equation and roots
print("\nThe Quadratic Equation ",qe, " have:")
print("Root 1=",int(root1))
print("Root 2=",int(root2))
except ValueError:
print("Roots cannot be calculated due to wrong coefficients")```