Selection Control Structure - Assignment 2
Problem Statement
Implement a program to solve a quadratic equation.
Find the discriminant value using the formula given below.
discriminant = b2 - 4ac
If the discriminant is 0, the values of both the roots will be same. Display the value of the root.
If the discriminant is greater than 0, the roots will be unequal real roots. Display the values of both the roots.
If the discriminant is less than 0, there will be no real roots. Display the message "The equation has no real root"
Use the formula given below to find the roots of a quadratic equation.
x = (-b ± discriminant)/2a
Sample Input and Output
class Tester{
public static void main(String[]args){
int a=1,b=4,c=6;
float dis=0f;
float x1=0f;
float x2=0f;
dis=(float)((b*b)-4*a*c);
System.out.println(dis);
if(dis==0)
{
System.out.println(":");
}
else if(dis>0)
{
System.out.println(":");
}
else{
System.out.println("The equation has no real roots ");
}
x1=(float)((-b+dis)/(2*a));
x2=(float)((-b-dis)/(2*a));
System.out.println(x1);
System.out.println(x2);
}
}
No comments:
Post a Comment