0% found this document useful (0 votes)
15 views3 pages

Cit212 Assignment

Uploaded by

victorychijioke3
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)
15 views3 pages

Cit212 Assignment

Uploaded by

victorychijioke3
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/ 3

/**

* Name:
* Dept: Software Engineering
* Reg No: 202213
*/

Package CIT202SN00
import java.util.Scanner;

public class CIT202SN00{


public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);

// Prompt the user for coefficients a, b, and c


System.out.print("Enter coefficient a: ");
double a = scanner.nextDouble();

System.out.print("Enter coefficient b: ");


double b = scanner.nextDouble();

System.out.print("Enter coefficient c: ");


double c = scanner.nextDouble();

// Calculate the discriminant


double discriminant = b * b - 4 * a * c;

// Determine the nature of the roots


if (discriminant > 0) {
// Two real and distinct roots
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("The roots are real and distinct:");
System.out.println("the root are:" + root1 + " and ")
System.out.println("Root 2: " + root2);
} else if (discriminant == 0) {
// One real root (double root)
double root = -b / (2 * a);
System.out.println("The root is real and repeated:");
System.out.println("Root: " + root);
} else {
// Complex roots
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
System.out.println("The equation has no real roots, the are complex:");
System.out.println("Root 1: " + realPart + " + " + imaginaryPart + "i");
System.out.println("Root 2: " + realPart + " - " + imaginaryPart + "i");
}

// Close the scanner


scanner.close();
}
}

Result
--- exec:3.1.0:exec (default-cli) @ CIT202SN00 ---
Enter coefficient a: 3
Enter coefficient b: 4
Enter coefficient c: 2
The equation has no real roots, the are complex:
Root 1: -0.6666666666666666 + 0.47140452079103173i
Root 2: -0.6666666666666666 - 0.47140452079103173i
BUILD SUCCESS

You might also like