Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Quadratic program

import java.util.Scanner;

public class QuadraticProgram {

   public static void main(String[] args) {

      // declare variables
      int a, b, c;
      int desc ;
      int root1, root2 ;
      int realPart, imaginaryPart;

      // create Scanner class object 
      // to read inputs
      Scanner scan = new Scanner(System.in);
      // read inputs
      System.out.print("Enter coefficients "
		+ "(a, b, and c values): ");
      a = scan.nextInt();
      b = scan.nextInt();
      c = scan.nextInt();

      // display Quadratic equation
      System.out.print("The quadratic equation: ");
      System.out.format("%d*x^2 + %d*x + %d = 0
",
                  		a, b, c);

      // calculate discriminant
      desc = (b*b) - (4*a*c);

      // find roots
      if(desc > 1) {
         // both roots are real and different 
         root1=(-b+(int)Math.sqrt(desc))/2*a;
         root2=(-b-(int)Math.sqrt(desc))/2*a;
         // display roots
         System.out.println("Roots are = "+ 
	    		root1 + ", "+ root2);
      } 

      else if(desc == 0) {
         // both roots are real and equal 
         root1=(-b+(int)Math.sqrt(desc))/2*a;
         root2 = root1;
         // display roots
         System.out.println("Roots are = "+ 
	    		root1+ ", "+ root2);
      } 

      else {
         // roots are complex and different
         realPart = -b/(2*a);
         imaginaryPart=(int)Math.sqrt(desc)/(2*a);
 	 System.out.format("root1 = %d + i(%d)
",
			realPart, imaginaryPart);
	 System.out.format("root2 = %d - i(%d)
",
			realPart, imaginaryPart);
      }

      // close Scanner class object
      scan.close();
   }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java startActivity crash 
Java :: find the length of jtextfeild in java 
Java :: candies price hackerearth solution in java 
Java :: snapshot for loop android java 
Java :: fibonacci series 
Java :: add days to a date java 
Java :: Uri/beecrowd problem no 1118 solution in Java 
Java :: play default message ringtone android studio 
Java :: Java TreeMap Example remove() 
Java :: naming convention for selenium java automation 
Java :: setting up javafx in eclipse 
Java :: java package 
Java :: Build path specifies execution environment JavaSE-1.8. There are no JREs installed in the workspace that are strictly compatible with this environment. 
Java :: += in java 
Java :: || in java 
Java :: final class java 
Java :: java jcombobox selected item changed 
Java :: search in 2d matrix 
Java :: import class from package java 
Java :: Access HashMap Elements 
Java :: Mila Kunis 
Java :: java dato numero big starkoverflow 
Sql :: delete all data in neo4j 
Sql :: brew restart mysql 
Sql :: running query in redshift 
Sql :: uninstall mysql on ubuntu 
Sql :: postgresql generate uuid 
Sql :: force drop all tables postgres 
Sql :: remove accented characters from string sql 
Sql :: oracle list packages 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =