Thursday, December 29, 2011

Program to find roots of quadratic equation using java

Save below code as roots.java


CODE:


import java.io.*;
import java.math.*;
import javax.swing.JOptionPane;
class roots
{
public static void main(String args[])
{
String n1,n2,n3;
float a,b,c,r1,r2;
double root1,root2,d;
n1=JOptionPane.showInputDialog("Enter the value of a in ax^2+bx+c=0");
n2=JOptionPane.showInputDialog("Enter the value of b in ax^2+bx+c=0");
    n3=JOptionPane.showInputDialog("Enter the value of c in ax^2+bx+c=0");
a=new Float(n1);
b=new Float(n2);
c=new Float(n3);
d=b*b-4*a*c;
if(d<0)
{  
  d=Math.sqrt(-d);
  JOptionPane.showMessageDialog(null,"The root1 is :(-"+b+"+i("+d+"))/"+2*a,"Result",JOptionPane.PLAIN_MESSAGE);
  JOptionPane.showMessageDialog(null,"The root1 is :(-"+b+"-i("+d+"))/"+2*a,"Result",JOptionPane.PLAIN_MESSAGE);
}
else
{
  root1=(Math.sqrt(d)-b)/2*a;
  root2=-(Math.sqrt(d)+b)/2*a;
  JOptionPane.showMessageDialog(null,"The root1 is "+root1,"Result",JOptionPane.PLAIN_MESSAGE);
  JOptionPane.showMessageDialog(null,"The root2 is "+root2,"Result",JOptionPane.PLAIN_MESSAGE);
    }
}     
}

//End of program





Output:






To run this java program you should first install jdk(Java Development kit).
you can download it from here.




And also can download this java code and directly execute by simply clicking on .bat file in the rar file downloaded


Download here
Password for rar is:letuscodeit




Know about:
-JOptionPane

No comments:

Post a Comment