Java Program To Print All Real Solutions To The Quadratic Eq

Ram Pothuraju
Quadratic.java

import java.io.*;

class Quadratic
{
    public static void main(String args[]) throws Exception
    {

        double a,b,c;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
        System.out.println("Enter the value of a:");
        a=Double.parseDouble(br.readLine());
       
        System.out.println("Enter the value of b:");
        b=Double.parseDouble(br.readLine());
       
        System.out.println("Enter the value of c:");
        c=Double.parseDouble(br.readLine());
       
        double delta=(b*b)-(4*a*c);
       
        if(a==0)
        {
            System.out.println("No solution possible");
        }
        else if ( delta == 0 )
        {
            double root = - b / (2 * a);
            System.out.println("The Real roots are equal:" + root );
        }

        else if(delta < 0)
        {
                System.out.println("The roots are imaginary");
        }
        else
        {
            double Root1 = (- b + Math.sqrt(delta)) / (2 * a);
            double Root2 = (- b - Math.sqrt(delta)) / (2 * a);
            System.out.println("The Real roots are unequal " + Root1 + " and " + Root2);
        }  
    }
}


OUTPUT



javac Quadratic.java

java Quadratic
Enter the value of a:
2
Enter the value of b:
5
Enter the value of c:
3
The Real roots are unequal -1.0 and -1.5

java Quadratic
Enter the value of a:
0
Enter the value of b:
1
Enter the value of c:
2
No solution possible

java Quadratic
Enter the value of a:
1
Enter the value of b:
2
Enter the value of c:
1
The Real roots are equal: -1.0
    

Post a Comment

0Comments

Post a Comment (0)