Saturday 26 March 2016

Roots of Quadratic Equation

Code:


import java.util.Scanner;
class Quadratic{
    public static void main(String[] args) {
        int a,b,c;
        System.out.println("Enter the three values of the equation");
        Scanner in=new Scanner(System.in);
        System.out.print("\nEnter a: ");
        a=in.nextInt();
        System.out.print("Enter b: ");
        b=in.nextInt();
        System.out.print("Enter c: ");
        c=in.nextInt();

        double temp=Math.sqrt(b*b-4*a*c);
        double root1=(-b+temp)/(2*a);
        double root2=(-b-temp)/(2*a);
        System.out.println("Roots of the Quadratic Equation are: "+root1 +" and "+root2+"\n");
    }
}

Output:


No comments:

Post a Comment