₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,280 members, 8,421,147 topics. Date: Friday, 05 June 2026 at 08:48 PM

Toggle theme

Java Program To Solve Quadratic Equation-dealwap - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingJava Program To Solve Quadratic Equation-dealwap (5971 Views)

1 Reply (Go Down)

Java Program To Solve Quadratic Equation-dealwap by D34lw4p(op): 6:39pm On Mar 07, 2016
Hello coders i was actually writing some codes to solve quadratic equation with JAVA, below is what i came up with.


public class QuadraticEquationSolver
{
public static void main(String[] args)
{
/*Suppose our Quadratic Equation to be solved
* is 2x2 + 6x + 4 = 0 .
* (Assuming that both roots are real valued)
*
* General form of a Quadratic Equation is
* ax2 + bx + c = 0 where 'a' is not equal to 0
*
* Hence a = 2, b = 6 and c = 4.
*/
int a =2;
int b =6;
int c =4;
//Finding out the roots
double temp1 = Math.sqrt(b * b -4* a * c);
double root1 = (-b + temp1) / (2*a) ;
double root2 = (-b - temp1) / (2*a) ;
System.out.println( "The roots of the Quadratic Equation \"2x2 + 6x + 4 = 0\" are "+root1+" and "+root2);
}
}
Re: Java Program To Solve Quadratic Equation-dealwap by Fulaman198(m): 6:44pm On Mar 07, 2016
D34lw4p:
Hello coders i was actually writing some codes to solve quadratic equation with JAVA, below is what i came up with.


public class QuadraticEquationSolver
{
public static void main(String[] args)
{
/*Suppose our Quadratic Equation to be solved
* is 2x2 + 6x + 4 = 0 .
* (Assuming that both roots are real valued)
*
* General form of a Quadratic Equation is
* ax2 + bx + c = 0 where 'a' is not equal to 0
*
* Hence a = 2, b = 6 and c = 4.
*/
int a =2;
int b =6;
int c =4;
//Finding out the roots
double temp1 = Math.sqrt(b * b -4* a * c);
double root1 = (-b + temp1) / (2*a) ;
double root2 = (-b - temp1) / (2*a) ;
System.out.println( "The roots of the Quadratic Equation \"2x2 + 6x + 4 = 0\" are "+root1+" and "+root2);
}
}
You need to place brackets (I mean parentheses) around the -4 *a, so like this (-4 * a). Otherwise the equation will come out incorrectly
Re: Java Program To Solve Quadratic Equation-dealwap by mbatuku1: 7:56pm On Mar 07, 2016
You will be needing the general formula, I'm sure of that. But I can't even see it in your code.
Re: Java Program To Solve Quadratic Equation-dealwap by Fulaman198(m): 4:14am On Mar 08, 2016
mbatuku1:
You will be needing the general formula, I'm sure of that. But I can't even see it in your code.
He has it: unless if you are referring to the ax^2 + bx + c

he has the code as follows:

double temp1 = Math.sqrt(b * b -4* a * c);
double root1 = (-b + temp1) / (2*a) ;
double root2 = (-b - temp1) / (2*a) ;
Re: Java Program To Solve Quadratic Equation-dealwap by dotman2460: 5:02am On Mar 08, 2016
Weldone OP.

One thing I'll like to add is, you should be aware that b * b -4* a * c will not always return a positive number, such as imaginary roots. So at the barest minimum you need an if else in your code.

You mind coding v1.1 of your solver? smiley wink
Re: Java Program To Solve Quadratic Equation-dealwap by Fulaman198(m): 10:10am On Mar 08, 2016
dotman2460:
Weldone OP.

One thing I'll like to add is, you should be aware that b * b -4* a * c will not always return a positive number, such as imaginary roots. So at the barest minimum you need an if else in your code.

You mind coding v1.1 of your solver? smiley wink
He doesn't have his code setup to prompt the user for input. He already has pre-set values for his code. This looks like an introductory assignment to a class in my opinion.
Re: Java Program To Solve Quadratic Equation-dealwap by dotman2460: 1:41pm On Mar 08, 2016
That's right.

Fulaman198:
He doesn't have his code setup to prompt the user for input. He already has pre-set values for his code. This looks like an introductory assignment to a class in my opinion.
Re: Java Program To Solve Quadratic Equation-dealwap by HCpaul(m): 8:07pm On Mar 08, 2016
I can't see your code, but you have to pass arguements in your parenthesis in a defined form.

You've declared the math standard library and your OOP structure. You will have to calculate your discriminant properly with correct parenthesis:
X = (-b +or- SQRT((b^2) - (4 * a * c))) / 2 * a

Note: First calculate your discriminant.
Re: Java Program To Solve Quadratic Equation-dealwap by mexzony: 9:22pm On Mar 08, 2016
A quadratic equation has what we call a determinant and depending on if its positive negative or zero.the equation can either have one real root two distinct roots or no roots at all can't remember exactly but I coded one in c# as an exercise.
Re: Java Program To Solve Quadratic Equation-dealwap by abilshay(m): 10:00am On Jul 14, 2017
This program isn't ok
1 Reply

Please HELP Me With This Java ProgramJava Program To Upload Picture On A Gui PlatformI Need Serious Help On My Java Program234

Im A Java Expert Here To Teach Those Who Wants To Learn.Python Smtp ScannerMy UI/UX Design Collection