0% found this document useful (0 votes)
2 views

Java project

This Java program calculates the roots of a quadratic equation based on user-provided coefficients a, b, and c. It ensures that coefficient 'a' is not zero and handles invalid inputs gracefully. The program determines the nature of the roots (real, equal, or complex) and displays the results accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java project

This Java program calculates the roots of a quadratic equation based on user-provided coefficients a, b, and c. It ensures that coefficient 'a' is not zero and handles invalid inputs gracefully. The program determines the nature of the roots (real, equal, or complex) and displays the results accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

JOptionPane;
public class quadratic { public static void main(String[] args)
{ double a = 0, b = 0, c = 0;
// Validate coefficient 'a' is not zero
while (true) { try
{
String inputA = JOptionPane.showInputDialog(null, "Enter coefficient a (must not be 0):");
if (inputA == null) return;
// User cancelled a =
a= Double.parseDouble(inputA);
if (a == 0) {
System.out.println("Coefficient 'a' cannot be zero.");
} else {
break;
}
} catch (NumberFormatException e)
{ System.out.println( "Please enter a valid number for a.");
}
}
// Get coefficient b
while (true)
{ try
{ String inputB = JOptionPane.showInputDialog(null, "Enter coefficient b:");
if (inputB == null) return;
b = Double.parseDouble(inputB);
break;
} catch (NumberFormatException e)
{ System.out.println("Please enter a valid number for b.");
}
}
// Get constant c
while (true)
{ try
{ String inputC = JOptionPane.showInputDialog(null, "Enter constant c:");
if (inputC == null) return;
c = Double.parseDouble(inputC);
break;
} catch (NumberFormatException e) {

System.out.println("Please enter a valid number for c.");


}
}
// Quadratic equation
String equation = a + "x² + " + b + "x + " + c + " = 0";
double discriminant = b * b - 4 * a * c;
String nature;
String roots;
if (discriminant == 0)
{ double root = -b / (2 * a);
nature = "Two real, equal and rational roots.";
roots = "Root = " + root; }
else if (discriminant > 0)
{ double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
nature = "Two real and unequal roots."; roots = "Root 1 = " + root1 + "\nRoot 2 = " + root2;
} else { double realPart = -b / (2 * a);
double imagPart = Math.sqrt(-discriminant) / (2 * a);
nature = "Two complex (imaginary) roots.";
roots = "Root 1 = " + realPart + " + " + imagPart + "i\n" + "Root 2 = " + realPart + " - " + imagPart
+ "i";
} String result = "Quadratic Equation:\n" + equation + "\n\nNature of Roots:\n" + nature + "\n\
nCalculated Roots:\n" + roots;
System.out.println(result+" Quadratic Roots");
}
}

You might also like