QuestionAsk Questions?

program to find roots of a quadratic equation?

View Answers

December 23, 2010 at 12:05 PM

Hi Friend,

Try the following code:

import java.util.*; class FindRootsOfQuadraticEquation{ public static void main(String args[]) throws Exception{ double root,root1,root2; Scanner input=new Scanner(System.in); System.out.println("Quadratic Equation is ax2+bx+c"); System.out.println("Enter value of a"); int a=input.nextInt(); System.out.println("Enter value of b"); int b=input.nextInt(); System.out.println("Enter value of c"); int c=input.nextInt(); int d=(b*b)-(4*a*c); if(d>0) { root1=(-b + Math.sqrt(d))/(2*a); root2=(-b - Math.sqrt(d))/(2*a); System.out.println("Roots are distinct"); System.out.println( root1 + "," + root2); } else if(d==0) { root=(-b)/(2*a); System.out.println("Only one root is distinct"); System.out.println(root); } else if(d<0) { System.out.println("Roots are imaginary"); } } }

Thanks

Related Tutorials/Questions & Answers:

Advertisements