Monday, 5 September 2016

Write a java program that prints all real solutions to the quadratic equation

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 solutions.

                   The program for find all the real solution to a quadratic equation contains the following steps
  • Read three integer values
  • Find the discriminant d using the formula b*b-4*a*c
  • Find the roots base on discriminant

import java.util.*;  
 class Quadratic   
 {  
 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);  
 }  
 }  
 }  

0 comments:

Post a Comment

Advertisement

Find Us On Facebook

C Programming

More

C++ Tutorial

More

Java Tutorial

More

Computer Basics

More

MS Office

More

Database Management

More
Top