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

Write a Java program that prints all real and imaginary solutions to the quadratic equation

Week – 1

a) Aim: To Write a Java program that prints all real and

imaginary solutions to the quadratic equation ax2+ bx +c = 0.

Description :
According to Linear Algebra of Quadratic Equations, The roots of a quadratic equation aX2+bX+c=0 depends on its discriminant values.The discriminant value d is calculated using the formula,
d=b2-4ac
i.                    If d=0 then the roots are real and equal and the roots are  -b/4a and –b/4a.
ii.                  If d>0 then the roots are real and distinct and the roots are (-b+(b^2 –  4ac)^1/2) / 2a
and    (-b-(b^2 –  4ac)^1/2) / 2a
iii.                If d<0 then the roots  are imaginary.
Based on this formulas, we are finding the roots of a quadratic equation.

Source Code :

import java.io.*;
import java.util.*;
class week2a
{
public static void main(String ar[])
{
int a,b,c,d;
Scanner s=new Scanner(System.in);
System.out.print(“The Quadratic Equation is of the                           form ax2+bx+c=0. n please enter values na =  “);
a=s.nextInt();
System.out.print(“nb = “);
b=s.nextInt();
System.out.print(“nc= “);
c=s.nextInt();
System.out.println(“The quadratic equation you                              entered is “+a+”x2+”+b+”x+”+c+”=0”);
System.out.print(“Its roots are   “);
d=b*b-4*a*c;
if(d>0)
{
System.out.println(“Real and distinct”);
double rt1=(-b+Math.sqrt(d))/(2*a);
double rt2=(-b-Math.sqrt(d))/(2*a);
System.out.print(“Roots are  “+rt1 +”    “+rt2);
}
else if(d==0)
{
System.out.println(“Real and equal”);
double rt1=(-b)/(2*a);
double rt2=(-b)/(2*a);
System.out.print(“Roots are  “+rt1 +”    “+rt2);
}
else if(d<0)
{
System.out.println(“Imaginary”);
}
}
}

Expected output :

The Quadratic Equation is of the form ax2+bx+c=0.
a=1
b=2
c=1
The quadratic equation you entered is 1×2+2x+1=0
Its roots are Real and Equal

Roots are -1.0    -1.0

x
• site: https://www.myprogrammingschool.com/

Write a Java program that prints all real and imaginary solutions to the quadratic equation

### Have any Question or Comment?

#### 3 comments on “Write a java program that prints all real and imaginary solutions to the quadratic equation”

[…] Public class JavaExample { public static void main (String [] args) { int count = 7, num1 = 0, num2 = 1; System.out.print (“” + count + “Fibonacci series of numbers:”); int i = 1; While (i <= count) { System.out.print (num1 + “”); int sumOfPrevTwo = num1 + num2; num1 = num2; num2 = sumOfPrevTwo; i ++; } } } Output: Fibonacci series of 7 numbers: 0 1 1 2 3 5 8 Missing Number in java with example Write a Java program that reads a file and displays the file on the screen with a line number before each line Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text Java program for sorting a given list of names in ascending order Write a java program to perform multiplication of two matrices write a java program that print the fibonacci series for a give number. Write a Java Program that find the factorial of a number Write a Java program that prints all real and imaginary solutions to the quadratic equation […]

[…] Source code:   import java.io.*; import java.util.*; class Area { public static void main(String []args) { Scanner ob=new Scanner(System.in); int l,h; l=ob.nextInt(); h=ob.nextInt(); System.out.println(“enter value l and h”); int Rec=l*h; System.out.println(“Area of rectangle:”+Rec); } } Missing Number in java with example Write a Java program that reads a file and displays the file on the screen with a line number before each line Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text Java program for sorting a given list of names in ascending order Write a java program to perform multiplication of two matrices write a java program that print the fibonacci series for a give number. Write a Java Program that find the factorial of a number Write a Java program that prints all real and imaginary solutions to the quadratic equation […]

[…] in this  program user will not  enter number form the keyboard. Value is fixed and you can see out put on the screen Source Code import java.io.*; import java.util.*; class Add { public static void main(String[] args) { //Scanner ob=new Scanner(System.in); System.out.println(“given value of a=5 and b=8”); int a=5 int b=8 int c=a+b; System.out.println(“sum of c:”+c); } } Missing Number in java with example Write a Java program that reads a file and displays the file on the screen with a line number before each line Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text Java program for sorting a given list of names in ascending order Write a java program to perform multiplication of two matrices write a java program that print the fibonacci series for a give number. Write a Java Program that find the factorial of a number Write a Java program that prints all real and imaginary solutions to the quadratic equation […]