# Help with linear equations program

Page 1 of 1

## 4 Replies - 479 Views - Last Post: 07 November 2017 - 08:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407449&amp;s=8fc726a35b1170b60af7bd8c06df740b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 camerontoma

Reputation: 0
• Posts: 7
• Joined: 07-November 17

# Help with linear equations program

Posted 07 November 2017 - 10:04 AM

Here is the assignment
Program Description: Write a program that solves the following linear equations in the form Ax + B = 0.

4x - 5 = 0
7x + 12 = 0
0x + 0 = 0
0x + 7 = 0
3.5x - 1.6 = 0
5x + 1.3 = 0

Read A and B from the keyboard. Be sure to account for all possible cases; that is:

• When A and B are both zero
• When A is non-zero
• When A is zero and B is non-zero

Required Statements: input, output, loop control, decision making, Be sure to run all of the test cases including at least one test case of your own that produces a different result from those given..

here is my code

```[import java.io.*;
import java.util.*;
public class prog213g{
public static void main (String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Enter A:");
double a = scan.nextDouble();
System.out.println("Enter B:");
double b = scan.nextDouble();

do{
double x= (-(b/a));
System.out.println(x);
}while(a>=1);
do{
double x= (-(b/a));
if(a==0 && b==0){
System.out.println("There are infinite solutions");
}
}while(a==0 && b==0);
do{
double x= (-(b/a));
if(a==0){
System.out.println("There are no solution");
}
}while(a==0);
}
}]
```

the loop keeps running and running

Is This A Good Question/Topic? 0

## Replies To: Help with linear equations program

### #2 modi123_1

• Suitor #2

Reputation: 14175
• Posts: 56,782
• Joined: 12-June 08

## Re: Help with linear equations program

Posted 07 November 2017 - 10:13 AM

Your conditions all seem to be reliant on some variable.. if that variable never changes then the condition is always true and it won't stop. Perhaps reevaluate the conditions and/or where you should be updating variables.

### #3 camerontoma

Reputation: 0
• Posts: 7
• Joined: 07-November 17

## Re: Help with linear equations program

Posted 07 November 2017 - 10:17 AM

I changed my code:
```[import java.io.*;
import java.util.*;
public class prog213g{
public static void main (String args[]){
Scanner scan = new Scanner(System.in);
double a;
double b;
do{
System.out.println("Enter A:");
a = scan.nextDouble();
System.out.println("Enter B:");
b = scan.nextDouble();
double x= (-(b/a));
if(a==0 && b==0){
System.out.println("There are infinite solutions");
}
if(a==0){
System.out.println("There are no solution");
}
System.out.println(x);
}while(a>-10000);

}
}]
```

but when I enter the zero for a or the zero for a and b i get this

Enter A:
0
Enter B:
0
There are infinite solutions
There are no solution
NaN
Enter A:
0
Enter B:
7
There are no solution
-Infinity

I am supposed to only get this

Enter A: 0
Enter B: 0
0x +0 = 0-->There are infinite solutions

Enter A: 0
Enter B: 7
0x +7 = 0-->There are no solutions

modi123_1, on 07 November 2017 - 10:13 AM, said:

Your conditions all seem to be reliant on some variable.. if that variable never changes then the condition is always true and it won't stop. Perhaps reevaluate the conditions and/or where you should be updating variables.

This post has been edited by modi123_1: 07 November 2017 - 10:18 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.

### #4 snoopy11

• Engineering ● Software

Reputation: 1467
• Posts: 4,726
• Joined: 20-March 10

## Re: Help with linear equations program

Posted 07 November 2017 - 12:10 PM

Why do you think there are infinite solutions to a linear equation at all ?

4x +32 = 0

0x + 76 = 0

Answer is A cannot be zero

50x + 0 = 0

Its a series of if .. if else statements you need not do while loops...

### #5 camerontoma

Reputation: 0
• Posts: 7
• Joined: 07-November 17

## Re: Help with linear equations program

Posted 07 November 2017 - 08:07 PM

The directions of the program say to output that when those 0's are put in. Also, a loop is required.

This post has been edited by ndc85430: 07 November 2017 - 11:43 PM
Reason for edit:: Removed quote of previous post. Just press "Reply".