First, there appears to be a typo in your question. Apparently your formula isn’t A^2-B^2=(A-B)+(A+B)
, as the beginning of your question suggests; the example shown is doing multiplication on the right-hand side of the equation.
Here’s a very basic example of how to do this.
int a = ReadInt("Enter value for A: "); // see end of answer for `ReadInt` definitions
int b = ReadInt("Enter value for B: ");
Console.WriteLine("{0}^2-{1}^2=({0}-{1})*({0}+{1})", a, b);
Console.WriteLine("{0}-{1} = ({2})*({3})", a * a, b * b, a - b, a + b);
Console.WriteLine("{0} = {1}", a * a - b * b, (a - b) * (a + b));
The {0}
, {1}
, etc. in the output text are placeholders. They are explained in the MSDN article “Composite Formatting”. Basically, the zero-based numbers inside the curly brackets specify which of the arguments after the format text (first argument to Console.WriteLine
) will be printed in their place.
The above code makes a reference to some ReadInt
method. Here are two possible definitions for it:
-
Unsafe, non-validating variant which will throw an exception if the users enters something other than an in-range integral number:
static int ReadInt(string prompt) { Console.Write(prompt); return int.Parse(Console.ReadLine()); }
-
Safer, validating variant:
static int ReadInt(string prompt) { for (;;) { Console.Write(prompt); int result; if (int.TryParse(Console.ReadLine(), out result)) { return result; } else { Console.WriteLine("Invalid input! Please enter an integral number."); } } }
5
solved C# : multiplication identity, How do i get it to do that? [closed]