07 August 2014

Java Programs

1.            Write a Java program that prints all real solutions to the quadratic equation ax2+bx+c=0.Read in a,b,c and use the quadratic formula.If the discriminant b2-4ac is negative,display a message stating that there are no real roots . import java.util.Scanner; class Solutions { public static void main(String args[]) { int a,b,c; double x,y; Scanner s=new Scanner(System.in); System.out.println("Enter the values of a,b, and c"); a=s.nextInt(); b=s.nextInt(); c=s.nextInt(); double f=(b*b)-4*a*c; System.out.println("F value="+f); if(f<0) { System.out.println("No real roots"); } else { double l=Math.sqrt(f); System.out.println("Math.sqrt(f)="+l); x=((-b-l)/(2*a)); y=((-b+l)/(2*a)); System.out.println("Roots of given equation:"+x+"\t"+y); } } } Output: Enter...

Page 1 of 612345Next

Popular Posts



www.Java2Hari.blogspot.com. Powered by Blogger.

 
Design by Java2Hari | Bloggerized by Free Blogger Templates | Free Downloads