Table of Contents

**Find the real and imaginary number of the complex number**

In this program, we will learn to write a java program that prints all real and imaginary solutions to the quadratic equation ax^{2}+ bx +c = 0. Read in a, b, c, and use the quadratic formula to find real and imaginary numbers calculator.

### Algorithm of Real and Imaginary Numbers

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 is calculated using the formula, d=b2-4ac

- If d=0 then the roots are real and equal and the roots are -b/4a and –b/4a.
- 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
- If d < 0 then the roots are imaginary.

Based on these formulas, we are finding the roots of a quadratic equation.

### Source Code :

import java.io.*;

import java.util.*;

class Imaginary

{

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("b = "+"\n");

b=s.nextInt();

System.out.print("c= "+"\n");

c=s.nextInt();

System.out.println("The quadratic equation you entered is "+a+"+x2+"+b+"+x+"+c+"=0");

System.out.print("real and imaginary 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.

Please enter values

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

**Recommended Post:**

- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number

Get Salesforce Answers

**Tags:** real & imaginary parts of a complex number, real and imaginary roots, real and imaginary solutions

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