Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import static java.lang.System.*;
- import java.util.Scanner;
- import java.lang.Math;
- class Proj{
- public static void main (String str[]) throws IOException {
- double a;
- double b;
- double c;
- String response = null;
- Scanner scanner = new Scanner(System.in);
- System.out.println("Please enter a value for A");
- a = scanner.nextInt();
- System.out.println("Please enter a value for B");
- b = scanner.nextInt();
- System.out.println("Please enter a value for C");
- c = scanner.nextInt();
- double db = Math.pow(b,2); // B^2
- double eb = (4)*(a)*(c); //discriminant
- double fb = Math.sqrt((db) - (eb)); //inside sqrt
- double gb = (-b) + (fb); // +
- double hb = (-b) - (fb); // -
- double x1 = gb / (2*a); //x sol w addition
- double x2 = hb / (2*a); //x sol w subtraction
- if (Double.isNaN(fb)) { //Checks for no real solutions
- System.out.println("No Real Solutions");
- }
- if(fb == 0) { //Checks for 1 real solution
- System.out.println("One Real Solution");
- System.out.println("x = " + x1);
- System.out.println("Would you like to see a factored form?"); // Asks if you would like to see a factored form
- response = scanner.next();
- if (response.equalsIgnoreCase("Yes")) { //Checks for if "yes" was said
- double x5 = (-x1);
- if(x5 > 0) { // +
- System.out.println("(x + " + x5 + ")");
- }
- else {
- System.out.println("(x " + x5 + ")"); //-
- }
- }
- else {
- System.out.println("Okay");
- }
- }
- else if(fb > 0) { //Checks for 2 solutions
- System.out.println("x = " + x1); //Displays x1 value
- System.out.println("x = " + x2); //Displays x2 value
- System.out.println("Would you like to see a factored form?"); // Asks if you would like to see a factored form
- response = scanner.next();
- if (response.equalsIgnoreCase("Yes")) { //Checks for if "yes" was said
- double x3 = (-x1);
- double x4 = (-x2);
- System.out.println("Factored form: "); //Displays Factored Form
- if(x3 > 0) { // +
- System.out.println("(x + " + x3 + ")");
- }
- else {
- System.out.println("(x " + x3 + ")");
- }
- if(x4 > 0) { // -
- System.out.println("(x + " + x4 + ")");
- }
- else {
- System.out.println("(x " + x4 + ")");
- }
- }
- else { // if something other than yes is said
- System.out.print("Okay.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement