Use * to multiply numbers and / to divide them in C#. The operand types determine whether division produces an integer or a fractional result.
Multiply and divide in a console application
This complete example declares its variables and shows both integer and fractional division. Invariant culture makes the displayed decimal separator a period on every machine.
using System;
using System.Globalization;
class Program
{
static void Main()
{
int a = 6;
int b = 2;
Console.WriteLine(a * b); // 12
Console.WriteLine(a / b); // 3
Console.WriteLine(7 / 2); // 3
Console.WriteLine(-7 / 2); // -3
Console.WriteLine(((double)7 / 2).ToString(CultureInfo.InvariantCulture)); // 3.5
Console.WriteLine((7m / 2m).ToString(CultureInfo.InvariantCulture)); // 3.5
Console.WriteLine((a + 1) * b); // 14
Console.WriteLine(12 / 3 * 2); // 8
}
}
12
3
3
-3
3.5
3.5
14
8
Why does 7 / 2 return 3?
When both operands are integers, division produces an integer quotient and discards the fractional part toward zero. Thus 7 / 2 is 3, and -7 / 2 is -3.
To keep the fractional part, convert an operand before dividing: (double)7 / 2 is 3.5. Assigning 7 / 2 to a double variable gives 3.0 because integer division has already happened. Use the m suffix for decimal literals, as in 7m / 2m.
Operator precedence and parentheses
Multiplication, division, and remainder have the same precedence and group from left to right. They have higher precedence than addition and subtraction. Parentheses let you choose a different grouping: (6 + 1) * 2 is 14, while 6 + 1 * 2 is 8.
For example, 12 / 3 * 2 groups as (12 / 3) * 2 and gives 8. C# has no exponentiation operator; ^ performs exclusive OR. For powers of floating-point numbers, use Math.Pow.
Division by zero and overflow
Integer and decimal division by zero throw DivideByZeroException. With float or double, a nonzero finite value divided by zero gives an infinity, and zero divided by zero gives NaN.
Large integer products can exceed their type’s range. Use a checked expression when you want an OverflowException instead of overflow wrapping for integer multiplication. The exceptional integer quotient int.MinValue / -1 throws OverflowException even in an unchecked context.
Continue learning
Use the remainder operator to test even or odd integers. For numeric text, parse the string before calculating.
Reference
Microsoft’s C# arithmetic operators reference explains division, remainder, precedence, and overflow behavior.