Please have a look this site which might help you,

Converting math equations to C#[^]

Hi

I want to solve an equation in asp.net c#

Equation is

then

But

It shows an error like x is undefined....

Please help me in solving this.

With Regards

Thanks in Advance

Manju K

I want to solve an equation in asp.net c#

Equation is

```
int x0 = 5;
int x1 = 2;
```

then

```
y = (x-x0)/(x0-x1)
```

it should return like`y = (x-5)/3;`

But

It shows an error like x is undefined....

Please help me in solving this.

With Regards

Thanks in Advance

Manju K

Comments

please show all code. in your code above there is no int x;

Yes, it will give error as where are you assigining value for x??userinput or hardcoded?

i think he meant x as the user input and y as the return value.

i don't see you have been declare variable x, y

so that the compiler throws that error

to solve that error, you can declare x, y variable inside your function like:

or write an function like:

so that the compiler throws that error

to solve that error, you can declare x, y variable inside your function like:

```
int x = 100; // value 100 is an example
int y = 100; // value 100 is an example
int x0 = 5;
int x1 = 2;
```

....or write an function like:

```
public int MyEquation(int x)
{
int y;
// here is your code
return y;
}
```

v2

Comments

I want to make it as it should return an equation like y = (x-5)/3;

public int MyEquation(int x)

{

return (x - 5) / 3;

}

{

return (x - 5) / 3;

}

You need to tell us what exactly you are trying to acheieve here. With the given information, this can be very easily done through String.Format. But does it help you? I think it will not.

You need to implement a parser of your own which could solve the equations. One needs to be well aware of your requirements to help you out. Search google for "C# Equation Parser". It may give you some ideas to start with.

```
int x0 = 5;
int x1 = 3;
string originalString = "y = (x-{0})/({1})";
string updatedString = string.Format(originalString, x0, (x0 - x1));
```

You need to implement a parser of your own which could solve the equations. One needs to be well aware of your requirements to help you out. Search google for "C# Equation Parser". It may give you some ideas to start with.

v2

Comments

You are not getting my question

I want to make it as it should return like y = (x-5)/3;

I want to make it as it should return like y = (x-5)/3;

Yes, I got it right after posting my first reply. I have updated my answer.

the solution of this type of problem is treating with variables of an equation (not of program asp...etc ) as string and using post-fix notation to solve it with some change that you will determine the variable as it is with out value

e.g

(X-x0)/(x0-x1)---post-fix--> Xx0-x0x1-/

the X means that it's variable of an equation solve it using stack I'll post the solution later.....

v2

Programming languages (usually) don't directly support symbolic algebra manipulation, have a look at Computer Algebra Systems[^] page at Wikipedia to get an idea about what you are trying to do.

v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada
+1 416-849-8900 x 100