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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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(); int k=(b*b)-4*a*c; if(k<0) { System.out.println(“No real roots”); } else { double l=Math.sqrt(k); x=(-b-l)/2*a; y=(-b+l)/2*a; System.out.println(“Roots of given equation:”+x+” “+y); } } } |
Output:
Enter the values of a,b,c
1
5
6
Roots of given equation:-3.0 -2.0
Enter the values of a, b, c
1
2
2
No real solutions
Enter the values of a, b, c
1
3
2
Roots of given equation: -2.0 -1.0
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
….hello again….
hats off to u 4 publishing sumthin wich i really cant understand…i really dnt knw wat kind of thing u r
using….in d program…!!!!
plz make changes so dat student lik me can understand
…………plz………
Thanx alott.. keep posting such useful programs
Thanx alott.. keep posting such useful programs
tnks 4 d program.