---
title: "Logic to solve two or three variable equation using C# language"  
description: "Here is the simple logic to solve 3 equations of 3 variables.  Say   ax + by + cz + d=0  px + qy + rz + s=0  gx + hy + jk + l=0to solve these equation"  
author: "Abhishek Srivasatava"  
published: 2016-11-05  
updated: 2018-03-16  
canonical: https://www.mindstick.com/blog/11223/logic-to-solve-two-or-three-variable-equation-using-c-sharp-language  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# Logic to solve two or three variable equation using C# language

Here is the [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) [logic](https://www.mindstick.com/articles/331924/best-ways-to-improve-your-logic-building-skills-for-programming) to solve 3 equations of 3 variables.\

```
Say
ax + by + cz + d=0px + qy + rz + s=0gx + hy + jk + l=0
```

to solve these equation consider represent the equation as [matrix](https://www.mindstick.com/forum/2412/how-to-convert-matrix-to-uiimage) [form](https://www.mindstick.com/forum/6/multi-form-mfc-application) :

```
a        b        c        dp        q        r        sg        h        k        l
```

Step 1: [Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) calculate the determinant (Say U) of this matrix

```
a        b        cq        p        rg        h        kU = a * (q * k - r * h) - b * (p * k - r * g) + c * (p * h - q * g);
```

**Step 2:** Calculate determinant of below matrix say V

```
b        c        dq        r        sh        k        l
```

**Note 1: Before calculating don’t ignore the sign [convention](https://www.mindstick.com/forum/145551/what-is-naming-convention-and-its-types) of the matrix for all case. In this case b and d will be negative and C will be positive.**

```
  V = -b * (r * l - s * k) + c * (q * l - s * h) - d * (q * k - r * h);
```

**Step 3:** [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of x will be V divide by U.

Hence x= V/U

**Step 4:** Similarly we need to calculate Y and Z.

For Y consider this matrix

```
a        c        dp        r        sg        k        l
```

**Note 2: a and d will be positive c will be negative.**

```
Hence
y = (a*(r*l-s*k) -c*(p*l-s*g)+d*(p*k-r*g))/U;
```

For Z consider this matrix

```
a        b        dp        q        sg        h        l
```

**Note 3: a and d will be Negative and b will be positive.**

```
                Z = (-a * (q * l - s * h) + b * (p * l - s * g) - d * (p * h - q * g))/U;
```

Here is the complete [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to solve 3 equation of 3 three variable.

```
 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        { int a = 1, b = 1, c = 1, d = -6, p = 1, q = -1, r = 1, s = -2, g = 1, h =2, k=-1, l = -2;            int U, V;            decimal W,Y,Z;            U = a * (q * k - r * h) - b * (p * k - r * g) + c * (p * h - q * g);            V = b * (-r * l + s * k) + c * (q * l - s * h) + d * (-q * k + r * h);             try            {                W = V / U;                 V = a * (r * l - s * k) - c * (p * l - s * g) + d * (p * k - r * g);                Y = V / U;                 V = -a * (q * l - s * h) + b * (p * l - s * g) - d * (p * h - q * g);                Z = V / U;                Console.WriteLine("{0},{1},{2}", W, Y, Z);                Console.Read();            }            catch (DivideByZeroException e)            {                Console.WriteLine("Divide by
zero exception meet.{0}",e);            }        }     }} 
```

Similar [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) can be used to solve two equations of [two variables](https://www.mindstick.com/forum/236/how-can-i-swap-two-variables-without-using-a-third-variable).

```
Lets two equations as followsax+by+c=0px+qy+r=0Then x = (b * r - c * q) / (a * q- b * p);And y = ( r * a -c * p) / (a * q - b * p);
```

Here is the program to solve two equation of two variable.

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            int a = 3, b = -1, c = -7, p = 2, q = 3, r = -1;                       decimal x, y;             try            {                x = (b * r - c * q) / (a * q - b * p);                y = ( r * a -c * p) / (a * q - b * p);                            Console.WriteLine("{0},{1}", x,y);                Console.Read();            }            catch (DivideByZeroException e)            {                Console.WriteLine("Solution not possible",e);                Console.Read();            }        }     }} 
```

---

Original Source: https://www.mindstick.com/blog/11223/logic-to-solve-two-or-three-variable-equation-using-c-sharp-language

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
