Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,879 members, 7,817,590 topics. Date: Saturday, 04 May 2024 at 03:07 PM

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

Nairaland Forum / Science/Technology / Programming / Java Program To Solve Quadratic Equation-dealwap (5676 Views)

Please HELP Me With This Java Program / Java Program To Upload Picture On A Gui Platform / I Need Serious Help On My Java Program (2) (3) (4)

(1) (Reply) (Go Down)

Java Program To Solve Quadratic Equation-dealwap by D34lw4p(m): 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

1 Like

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)

Your Programming Experience And Your Languages. / My Just Completed Android E-voting App / Functional Programming For Java Programmer Scala Or Clojure?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.