C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Predefined: The C# language specification defines many predefined arguments (int, double) for the multiplication operator.
Int, uintDoubleHere: We compute the product of 100 and 5. The number 100 is a constant. The value 5 was determined at program runtime.
Note: Because one value was not known at compile-time, the C# compiler cannot compute the product of the operands statically.
C# program that multiplies numbers
using System;
class Program
{
static void Main()
{
//
// Use a constant local and a dynamically
// ... determined integer as operands.
//
const int operand1 = 100;
int operand2 = int.Parse("5");
//
// Compute the product and store it in a local variable.
//
int product = operand1 * operand2;
Console.WriteLine(product);
//
// You can check the value of a multiple expression in an if-statement.
//
if ((operand1 * operand2) == 500)
{
Console.WriteLine("Is equal");
}
//
// You can multiply a number against itself.
//
operand2 *= 2;
Console.WriteLine(operand2); // Now equal to 10 not 5.
}
}
Output
500
Is equal
10
Info: In the innermost statement, we use the multiplication operator and then use a ToString format pattern.
ForAlso: A new line is printed at the end of each nine numbers in the loop. This improves the output format.
C# program that creates multiplication table
using System;
class Program
{
static void Main()
{
// Loop through all operands in the multiplication table.
for (int a = 1; a < 10; a++)
{
for (int b = 1; b < 10; b++)
{
// Write the multiplied number.
Console.Write((a * b).ToString("00 "));
}
// New line.
Console.WriteLine();
}
}
}
Output
01 02 03 04 05 06 07 08 09
02 04 06 08 10 12 14 16 18
03 06 09 12 15 18 21 24 27
04 08 12 16 20 24 28 32 36
05 10 15 20 25 30 35 40 45
06 12 18 24 30 36 42 48 54
07 14 21 28 35 42 49 56 63
08 16 24 32 40 48 56 64 72
09 18 27 36 45 54 63 72 81
Tip: The C# language has additional rules for coding-specific operators not found in regular mathematics.
Note: At first strength reduction might not seem like a common optimization. We might not specify multiplication that often.
However: It is actually used for array accesses. These must address an element based on a multiple of the element size.
Therefore: Many array accesses and loop indexes may involve multiplications. And these are not explicit in the source code.
Finally: Compilers also try to understand data reuse to improve locality of reference in subsequent loops.