1. ## Simultaneous linear Equations

I need to make a Console Application which can calculate the values of the Constants " X " and " Y " for the 2 equations ( ax + by = c) and ( dx + ey = f ) .. Of Course the program will ask the user to enter the values for the variables ( a , b , c , d , e , f ). I've tried to make some code, but got stuck at the core of the code. Would anybody please help me?

my Code:

Code:
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1st_Exercise
{
class Program
{
static void Main(string[] args)
{
//Getting the constants
Console.WriteLine("\"dx + ey = f\" ,, \"ax +by = c\"\n");
Console.Write("F= ");
int F;
Console.Write("\nE= ");
int E;
Console.Write("\nD= ");
int D;
Console.Write("\nC= ");
int C;
Console.Write("\nB= ");
int B;
Console.Write("\nA= ");
int A;

//Code core
int x;
x = (A - D);

int y;
y = (B - E);

//Output
Console.WriteLine("\n\nX = " + x );
Console.WriteLine("\nY = " + y );

//Exit program
Console.WriteLine("\n\nPress any key to exit");

}
}
}```

2. You can brute force it in a nested for loop by running through, and checking, each combination of X and Y. Probably not the fastest way to do this though. To give you a rough idea:

Code:
```for(y=0; y<100; y++)
for(x=0; x<100; x++)
if((A*x + B*y) == C)
if((D*x + E*y) == F)
break;```

3. Would some one Put the Whole code please?

4. And what about solving the equations manually and implement it?
D*A*x + D*B*y == D*C
A*D*x + A*E*y == A*F
(D*B-A*E)*y == D*C - A*F

so if (D*B-A*E) != 0
y = (D*C - A*F) / (D*B-A*E)

same goes for x

in the else case - you have 2 possibilities: lines are the same (infinite number of solutions)
or - 2 line have no common point - so no solution is available

5. Originally Posted by dvd4alll
Would some one Put the Whole code please?
Just you. WE can help to fix errors...

6. That's my new Code .. is it ok ?

Code:
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1st_Exercise
{
class Program
{
static void Main(string[] args)
{
//Getting the constants
Console.WriteLine("\"dx + ey = f\" ,, \"ax +by = c\"\n");
Console.Write("F= ");
int F;
Console.Write("\nE= ");
int E;
Console.Write("\nD= ");
int D;
Console.Write("\nC= ");
int C;
Console.Write("\nB= ");
int B;
Console.Write("\nA= ");
int A;

//Code core
for (int y = 0; y < 100; y++)
{
for (int x = 0; x < 100; x++)
{
if ((A * x + B * y) == C)
{
if ((D * x + E * y) == F)
{

//Output
Console.WriteLine("\n\nX = " + x);
Console.WriteLine("\nY = " + y);
}
}
}
}

//Exit program
Console.WriteLine("\n\nPress any key to exit");

}
}
}```

7. I wrote something like this a few years ago in regular C that would solve a system of n equations with n unknowns using Cramer's rule. (Requires a little bit of linear algebra, but it's not too bad).

But if you're always going to have 2 equations and 2 unknowns, it is much simpler and you should be able to hard-code the solution as vart suggested. I don't suggest brute-forcing for a number of reasons including inefficiency and the cases where x and y are not integers.

8. Well, you can test the code if it works, and if it works, it's working. Not so hard?

9. Originally Posted by Livijn
Well, you can test the code if it works, and if it works, it's working. Not so hard?
haha, that was one fun/good thing you said there. I will use it in the future.