Ad Code

Quadratic Equation in Java.

Quadratic Equation in Java

The standard form of a quadratic equation is ax2+bx+c=0. 

A quadratic equation has two roots and the roots depend on the discriminant. In the above formula, (√b2-4ac) is called discriminant (d).

PROGRAM :-


 import java.util.Scanner;  
 public class qe
 { 
 public static void main(String[] Strings)   
 {  
 Scanner input = new Scanner(System.in);  
 System.out.print("Enter the value of a: ");  
 double a = input.nextDouble();  
 System.out.print("Enter the value of b: ");  
 double b = input.nextDouble();  
 System.out.print("Enter the value of c: ");  
 double c = input.nextDouble(); 
 double r1, r2; 
 double d= b * b - 4.0 * a * c;  
 if (d> 0.0)   
 { 
    r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);  
    r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a); 
 System.out.println("The roots are " + r1 + " and " + r2);  
 }   
 else if (d == 0.0)  
 {
    r1 = r2 = -b / (2.0 * a);
 System.out.println("The root is " + r1);  
 }   
 else   
 {  
 System.out.println("Roots are not real.");  
 }  
 }
 } 

OUTPUT :-

Enter the value of a: 2

Enter the value of b: 1

Enter the value of c: 1

Roots are not real.

         (OR)

Enter the value of a: 1

Enter the value of b: 5

Enter the value of c: 1

The roots are -0.20871215252208009 and -4.7912878474779195


Share with your friends, Thankyou

Post a Comment

0 Comments

Ad Code