C# Multiply Numbers

Star (asterisk) character

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.

This C# article shows aspects of multiplication. The multiply operator is covered.

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. The C# language specification defines many predefined arguments for the multiplication operator.

Program that multiplies numbers [C#]

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
Note

Overview. The program shown 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, while the number 5 was determined dynamically at runtime based on a string literal. Because one value was not known at compile-time, the C# compiler cannot compute the product of the operands statically (at compile-time).

The program also shows how you can 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, with additional rules for coding-specific operators that are 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 to print it to the screen. A new line is printed at the end of each nine numbers in the loop.

For Loops
Program that creates multiplication table [C#]

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
Google search engine

Study aid. You can see that the multiplication table is correct, at least if you happen to know your multiplications already. By checking Google, you can see that 8 times 7 is indeed 56. Therefore, we can see that the C# language and .NET Framework is able to correctly multiply numbers as well. This assumes that Google is also able to correctly multiply numbers.

Concepts

Note

Here we try to understand multiplication as an important part of compiler technologies and all programs, even those without explicit multiplications. Multiplications are somewhat more expensive than additions, and for this reason computer scientists have developed many strategies for reducing them, such as strength reduction for affine expressions.

Compiler Explanation

Strength reduction. The concept of 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, but it is actually used for array accesses, because array accesses 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. This means that many array accesses and loop indexes may involve multiplications.

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. To do this, vector sets are tested for linear independence, which can prove that data spaces are used more than once.

Summary

The C# programming language

We looked at multiplying numbers in the C# language, using both constants known at compile-time and integers that were determined during runtime. The multiplication operator in C# is the star * character, and it also contains a compound operator represented by the *= characters.

Number Examples
.NET