-2

How can I realize a equations like this in C# with custom inputs?

Input is amount of Legs, and the amount of animals.

animals: 35
legs: 94

x: Cows
y: Hens

Amount of animals
x + y = 35
y = 35 - x

Amount of legs
Hens: 2 legs
Cows: 4 legs

4x + 2y = 94

->

4x + 2y = 94
4x + 2(35 - x) = 94
4x + 70 - 2x = 94
2x = 24
x = 12

y = 35 - x = 35 - 12 = 23

-> 12 Cows -> 23 Hens

  • 1
    You want to display each step of the computation? – xdtTransform Dec 10 '19 at 09:44
  • 2
    This question is by far too broad. What have you tried? Where are you stuck? – MakePeaceGreatAgain Dec 10 '19 at 09:46
  • Try to do any research. We don't want to write the ready-to-use program for you, we are not programming service. – VillageTech Dec 10 '19 at 09:50
  • Are you wanting to know how to code quadratic equations in C#? The starting point is that you know that for class Cow.Legs == 4 & Hen.Legs == 2. Then do you wish to determine the number of Cows and Hens if the total number of animals is 35 and the total number of legs is 94? – ChrisBD Dec 10 '19 at 09:52
  • Is there any function which can do that? –  Dec 10 '19 at 09:54
  • I guess you already know about linear equation and thier matrices and vectors equivalent. There should be a lot of project around on the net solving this on fortran, basic, c, c++. The simple resolution of the equation could be done by all math library. – xdtTransform Dec 10 '19 at 09:55
  • related : [Best way to solve a linear equation in code](https://stackoverflow.com/questions/4383959/), [Solving a linear equation](https://stackoverflow.com/questions/769) – xdtTransform Dec 10 '19 at 09:56

1 Answers1

2

My solution applies if you have only these two types of animals.

int animals = 35;
int legs = 94;
int x = (legs - 2*animals)/2;
int y = animals -x;
Console.WriteLine(x + " Cows and " + y + " hens");

If you want to solve linear equation, then you can follow this link

Mahbub Moon
  • 491
  • 2
  • 8