TheDeveloperBlog.com


C# Multiply Numbers

Multiply. The multiply operator is an asterisk. It computes the product of two numbers. This is the same in the C# language as in many other programming languages. It obeys the operator precedence rules.


Example. In this program, we compute the product of two operands using the multiplication operator, which is expressed with the asterisk or star character. You can multiply any two numbers that can be implicitly converted to compatible types.

Note: The C# language specification defines many predefined arguments (int, double) for the multiplication operator.

IntDouble
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

This program computes the product of the numbers 100 and 5, which is equal to 500. The number 100 is a constant and is embedded as a value through its uses. The number 5 was determined dynamically at runtime based on a string literal.

String Literal

Note: Because one value was not known at compile-time, the C# compiler cannot compute the product of the operands statically.

Also, the program shows how to compare the value of a multiplication expression directly, without storing it in a local variable. Finally, the program multiplies the operand by 2 using the *= compound multiplication operator.


Operator precedence. The multiplication operator in the C# language has a greater precedence than do other operators such as plus or minus. Generally, the rules for operator precedence in the C# language are the same as those for standard arithmetic.

Tip: The C# language has additional rules for coding-specific operators not found in regular mathematics.


Table. Next we write a multiplication table program. It uses two nested for-loops. The numbers we loop over are in the range 1 to 9. In the innermost statement, we use the multiplication operator and then use a ToString format pattern.

Forstring.Format

Also: A new line is printed at the end of each nine numbers in the loop. This improves the output format.

Console.WriteLine
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

You can see that the multiplication table is correct, at least if you happen to already know your multiplications. By checking Google, you can see that 8 times 7 is 56. The .NET Framework is able to correctly multiply numbers.

Note: This assumes that Google is also able to correctly multiply. This is most likely a safe assumption.


Concepts. In programs, multiplications are more expensive than additions. For this reason computer scientists have developed many strategies for reducing them, such as strength reduction for affine expressions.

Compiler

Strength reduction refers to replacing an expensive (strong) expression, such as one that uses multiplication or division, with cheap operations such as addition. At first strength reduction might not seem like a common optimization.

However: It is actually used for array accesses. These must address an element based on a multiple of the element size.

Affine expressions and data reuse. Compiler technology also implements optimizations based on the concept of affine expressions, which are expressions in loops that are multiplied or added by a constant.

Therefore: Many array accesses and loop indexes may involve multiplications. And these are not explicit in the source code.

Compilers also try to understand data reuse to improve locality of reference in subsequent loops. Mathematics such as linear algebra helps scientists model what array locations are accessed in sequences of loops.

And: To do this, vector sets are tested for linear independence, which can prove that data spaces are used more than once.


Summary. We looked at multiplying numbers, using both constants known at compile-time, and integers that were determined during runtime. The multiplication operator is the star character. The compound operator is represented by the *= characters.