# help to build this java code for quadratic equation

Page 1 of 1

## 4 Replies - 854 Views - Last Post: 22 April 2012 - 08:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=276338&amp;s=1cba142fe333de41777ffa0cc15bc94a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aliaslam7163

Reputation: 0
• Posts: 3
• Joined: 22-April 12

# help to build this java code for quadratic equation

Posted 22 April 2012 - 06:21 PM

```import java.util.*;

public static void main(String[] args) {

double a, b ,c; //values obtained from keyboard to satisfy a binomial equation

System.out.println("Enter in the value of a = ");
a = getinput();

System.out.println("Enter in the value of b = ");
b = getinput();

System.out.println("Enter in the value of c = ");
c = getinput();

double numerator = Math.pow(b, 2) - (4*a*c);
double x1 = ((-1 * B)/> + Math.sqrt(numerator)) / (2 * a);
double x2 = ((-1 * B)/> - Math.sqrt(numerator)) / (2 * a);

if(numerator > 0){
System.out.println("X is " + x1);
System.out.println("X is " + x2);
}
else if (numerator == 0){
System.out.println("X is " + x1);
}

}

public static int getinput()
{
Scanner input; //bringing in the scanner class to obtain keyboard inputs from user
input = new Scanner(System.in);

return input.nextInt();

}

}

```

This post has been edited by pbl: 22 April 2012 - 07:47 PM

Is This A Good Question/Topic? 0

## Replies To: help to build this java code for quadratic equation

### #2 aliaslam7163

Reputation: 0
• Posts: 3
• Joined: 22-April 12

## Re: help to build this java code for quadratic equation

Posted 22 April 2012 - 06:29 PM

the smiley face is suppose to be b but i dont know why its displaying a smiley character

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: help to build this java code for quadratic equation

Posted 22 April 2012 - 07:53 PM

aliaslam7163, on 22 April 2012 - 09:29 PM, said:

the smiley face is suppose to be b but i dont know why its displaying a smiley character

Ya the browser nterprets b ) as a smiley don't worry about it or avoid to use a single B as variable name

And what is your question/problem ?
If my secondary scholl maths are rigth it -b +/ sqrt(b^2 - 4ac) not 1 - b

Sorry -1 * b == -b

### #4 aliaslam7163

Reputation: 0
• Posts: 3
• Joined: 22-April 12

## Re: help to build this java code for quadratic equation

Posted 22 April 2012 - 07:53 PM

well the quadratic function is implemented in the code as 2 steps. how can i fix that if my sqrt is less than zero. how can i stop it from failing.

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: help to build this java code for quadratic equation

Posted 22 April 2012 - 08:21 PM

Perform the calculation of X1 and X2 inside the if() that test the numerator so you won't do it if < 0