Quadratic equation solver with user input - Java Language Basics

Java examples for Language Basics:Operator

Description

Quadratic equation solver with user input

Demo Code

import java.util.Scanner;

public class Main {
    public static void main(String[] arg) {
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a,b,c of equation ax^2+bx+c=0:");
        double a = keyboard.nextDouble();
        double b = keyboard.nextDouble();
        double c = keyboard.nextDouble();

        double delta = b * b - 4.0 * a * c;
        double root1, root2;

        if (delta >= 0) {
            root1 = (-b - Math.sqrt(delta)) / (2.0 * a);
            root2 = (-b + Math.sqrt(delta)) / (2.0 * a);
            System.out.println("Two real roots:" + root1 + " " + root2);
        } else {//from w ww .  j av  a 2  s .co m
            System.out.println("No real roots");
        }
    }
}

Result


Related Tutorials