sivaramaiah  
 
  dsjava1 12/15/2017 12:09pm (UTC)
   
 

Quadratic equation

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

The roots of the quadratic equation, ax2 + bx + c = 0 are given by:

x = [–b ± √(b2 – 4ac) ]/2a The discriminant, d = (b2 – 4ac)

 

Case (1): When d is greater than zero, the two roots are real.

x1 = (b + √d )/2a and x2 = (–b – √d )/2a Case (2): When d is equal to zero, the two roots are equal.

 

x1 = x2 = – b /2a Case (3): When d is less than zero, the two roots are imaginary. Each of the two roots has two

 

parts: real-part-1, imaginary-part-1 and real-part-2, imaginary-part-2.
real-part-1, xr1 = –b/2a imaginary-part-1, xi1 = +√d /2a real-part-2, xr2 = –b/2a imaginary-part-2, xi2 = –√d /2a Program 1: Roots of a quadratic equation

 

 

-------------------------------------------------------------

import java.lang.Math;
import java.util.Scanner;
class QuadraticEquation {

 

double a, b, c; // coefficients
QuadraticEquation( double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;
}
public void roots()
{
if( a == 0.0 )
{ System.out.println("One root = " + (-c/b));
return;
}
double d = b*b - 4.0*a*c;  if(d < 0) // Roots are imaginary

 

{

System.out.println("There are no real solutions.");
return;
}
if(d == 0) // Roots are equal

{

System.out.println("Two roots are equal: " + (-b /(2.0*a)));
return;
}

// Roots are real

double x1 = (-b + Math.sqrt(d))/(2.0*a);

double x2 = (-b - Math.sqrt(d))/(2.0*a);
System.out.println("Roots are: " + x1 + ", " + x2);
}
}
//////////////////// QuadraticEquationDemo.java /////////////////
class QuadraticEquationDemo
{
public static void main(String[] args)
{
Scanner scr = new Scanner(System.in);
System.out.print(" a = ");
double a = scr.nextDouble();
System.out.print(" b = ");
double b = scr.nextDouble();
System.out.print(" c = ");
double c = scr.nextDouble();
QuadraticEquation qe = new QuadraticEquation(a, b, c);
qe.roots();
}
}

----------------------------------------------------------

Output of this program is as follows (for different values of a, b, and c):

 

a = 0
b = 4
c = 1
One root = -0.25
a = 1
b = 4
c = 4
Two roots are equal: -2.0
a = 1
b = 4
c = 8
There are no real solutions
a = 1
b = 4
c = 3 Roots are: -1.0, -3.0

 

 
  Menu Items
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

  add
HOT TOPICS
MCA PROJECT DETAILS ------------------------------------- Jntu all Lab manuals ------------------------------------- Jntu Syllabus Books ------------------------------------- Paper presentations ------------------------------------- Seminars ------------------------------------- Campus Papers ------------------------------------- Competetive Exams GATE ------------------------------------- GRE ------------------------------------- TOEFL ------------------------------------- IELTS ------------------------------------- CAT ------------------------------------- GMAT ------------------------------------- Templates ------------------------------------- Students Resume Preparation tips ------------------------------------- Job zone(Interview questions) ------------------------------------- Google Adsence html code ------------------------------------- Web sites --------x--------------x-----------
  Advertisement
  Advertisement

 


-----------------------------
  Offline Messages
  Adds
  Visitors Information
Today, there have been 67778 visitors (191360 hits) on this page!
=> Do you also want a homepage for free? Then click here! <= --------------------------------------------------