Computer Science, asked by shantimohapatro6149, 2 months ago

write java program that display the roota of a quadratic equation ax2+bx=0

Answers

Answered by kamalrajatjoshi94
1

Answer:

Program:-

import java.util.*;

public class Main

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

int a,b,c;

double x1=0,x2=0;

System.out.println("Enter a:");

a=in.nextInt();

System.out.println("Enter b:");

b=in.nextInt();

System.out.println("Enter c:");

c=in.nextInt();

if(b*b-4*a*c>=0)

{

x1=((-b)+(Math.sqrt(b*b-4*a*c)))/2.0;

x2=((-b)-(Math.sqrt(b*b-4*a*c)))/2.0;

System.out.println("The roots are possible:");

System.out.println("The first root="+x1);

System.out.println("The second root="+x2);

}

else

System.out.println("The roots are imaginary");

in.close();

}

}

  • The first root is like this

 {x}^{2}  + 5x + 6 = 0

It is possible and has solution -2,-3

(x+2)=0 or (x+3=0)

  • The second root is not possible

 {x}^{2}  + 2x + 3 = 0

Which is not possible so it gave the roots are imaginary.

  • The third root is also possible and the roots are equal.

More information:-

Quadratic equation formula:-

  \frac{ - b +  -  \sqrt{ {b}^{2}  - 4ac} }{2a}

Attachments:
Similar questions