Home
Answers
Viewqa
Java-Beginners
how to find the roots of polynomial equation of 'n'th degree in java

QuestionAsk Questions?

Posted in : Java Beginners

i am new to java field and i wanted to find the roots of nth degree polynomial equation pls help me. thanks.

View Answers

May 9, 2011 at 12:40 PM

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"); } } }

Related Tutorials/Questions & Answers:

Advertisements