As every conscientious algebra student knows the quadratic f
Solution
QuadraticEquationRoots.java
import java.util.Scanner;
public class QuadraticEquationRoots {
public static void main(String[] args) {
//Declaring variables
int a = 0,b = 0,c = 0,num;
//Scanner class object is used read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//This loop continues to execute until the method returns 2
while(true)
{
//Getting the number \'a\' entered by the user
System.out.print(\"\ Enter Number a:\");
a=sc.nextInt();
//Getting the number \'b\' entered by the user
System.out.print(\"Enter Number b:\");
b=sc.nextInt();
//Getting the number \'c\' entered by the user
System.out.print(\"Enter Number c:\");
c=sc.nextInt();
//calling the method by passing the a,b,c as inputs
num=printRealRoots(a,b,c);
/*checking the return value of the method is 2 or not
* if the method returned 2,Then program will exit.
* If not,program will continue to call the method printRealRoots()
*/
if(num==2)
break;
else
continue;
}
}
/* This method will calculate the roots of the quadratic equation
* Params : a,b,c
* Return : integer
*/
private static int printRealRoots(int a, int b, int c) {
//Declaring variables
double rootx=0,rooty=0;int val=0;
double discriminate=Math.pow(b, 2)-4*a*c;
double part1=Math.sqrt(Math.pow(b, 2)-4*a*c);
//If the discriminate is positive calculate the two real roots an return 2 to the caller
if(discriminate>0)
{
//calculating the two real roots
rootx=((-b+part1)/(2*a));
rooty=((-b-part1)/(2*a));
//Displaying the two real roots
System.out.printf(\"The Two roots are %.2f %.2f \",rootx,rooty);
val=2;
}
//If the discriminate is equal to zero calculate the single root an return 1 to the caller
else if(discriminate==0)
{
//calculating the single root
rootx= ((-b+part1)/(2*a));
//Displaying the single root
System.out.println(\"The Equation has one real root :\"+rootx);
val=1;
}
//If the discriminate is less than zero return 0 to the caller
else if(discriminate<0)
{
//Displaying the message
System.out.println(\"::No Real Roots::\");
val=0;
}
return val;
}
}
_________________________________________
Output:
Enter Number a:3
Enter Number b:2
Enter Number c:5
::No Real Roots::
Enter Number a:2
Enter Number b:8
Enter Number c:8
The Equation has one real root :-2.0
Enter Number a:1
Enter Number b:-3
Enter Number c:-4
The Two roots are 4.00 -1.00
_________________________Thank You


