Find the roots of a given polynomial
This is a program which finds the roots of a given polynomian in the form of ax2+bx+c, where a, b and c are the inputs given by the user. In this program there is a method check() which checks if the value of the discrimainate is a positive number, if it is positive then we will get real roots. This progam is designed using Java.
Description:
In the class poly, getit() method is used to assign the values to a,b, and c. The ckech() confirms whether the discriminate is a positive or a negative, the calc() calculates the value of the roots based on the getit() return value. further the calc() drives the disp() method which prints the result.
import java.util.Scanner; class poly { int a,b,c; double s1,s2,d; poly() { a=b=c=0; s1=s2=d=0.0; } void getit() { Scanner s=new Scanner (System.in); a=s.nextInt(); b=s.nextInt(); c=s.nextInt(); } boolean check() { d=(b*b)-(4.0*a*c); if(d>=0.0) return true; else return false; } boolean calc() { if(check()) { s1=((-b)+(Math.sqrt(d)))/(2.0*a); s2=((-b)-(Math.sqrt(d)))/(2.0*a); return true; } else return false; } void disp() { if(calc()) System.out.println("The Roots are: s1: "+s1+", "+s2); else System.out.println("The Roots are not Real."); } } class mainDriver { public static void main(String []args) { poly p =new poly(); p.getit(); p.disp(); } }
Description:
In the class poly, getit() method is used to assign the values to a,b, and c. The ckech() confirms whether the discriminate is a positive or a negative, the calc() calculates the value of the roots based on the getit() return value. further the calc() drives the disp() method which prints the result.
Comments
Post a Comment