Quadratic equations Real solutions in Java code
Write a Real solutions of Quadratic equations java code which will prints all real solutions of the user entered quadratic equation of form ax2+bx+c=0. Program should take the a,b,c values and use the quadratic formula to calculate real solutions.
If the Calculated discriminant which is b2-4ac is negative, Program should display output with the generated message as that there are no real solutions.
We can use sqrt functions for calculating Real solutions of Quadratic equations. Declare a.b,c and d as integer data types and e,f as double type.
Algorithm for Finding Real solutions of Quadratic equations in Java
- Take input from user as values of a,b,c .
- Print values to screen for user confirmation.
- Calculate b2-4ac .
- if b<0 no real Roots .
- else Apply these formulas :
e=((-b+Math.sqrt(d))/(2*a))
e=((-b-Math.sqrt(d))/(2*a))
- Print both the values to screen.
import java.lang.*; import java.util.*; public class Quadraticequation { public static void main(String args[]) { int a,b,c,d; double e,f; System.out.println("Enter a,b,c values"); Scanner sc = new Scanner(System.in); a=sc.nextInt(); b=sc.nextInt(); c=sc.nextInt(); System.out.println("Enterd values are a="+a+" b="+b+" c="+c); d=(b*b)-(4*a*c); if(d<0) { System.out.println("No real roots"); }else { e=((-b+Math.sqrt(d))/(2*a)); f=((-b-Math.sqrt(d))/(2*a)); System.out.println("Roots are "+e+" "+f); } } }
Leave a Reply