Multiplication and Division in C#

In C#, multiplication and division of numbers can be achieved pretty easily. To multiply or divide numbers using C# is really similar to multiplying and dividing numbers using other programming languages. In C#, the multiplication symbol used is the asterisk (*), so if you want to multiply a number by another number, you simply need to place the asterisk between them:

a = 6;
b = 2;
Console.WriteLine(a * b);

The output of the code above would be the result of a multiplied by b, or 6 x 2, which equals 12.

The symbol used to represent division is the forward slash (/). If you want to divide one number by another, you’ll need to place the forward slash character between them. Using the same values for a and b as in the example above, check out how to divide two numbers in C# below:

Console.WriteLine(a / b);

The output of the code above would be 3, because 6 divided by 2 equals 3.

It’s also worth nothing that the standard order of operations does apply to C# calculations, so if you’re writing a complicated expression, it’s important to keep this order (parentheses, exponents, multiplication, division, addition, and then subtraction) in mind:

a = 6;
b = 2;
c = 1;
d = (a+c) * b;
Console.WriteLine(d);

The value of d in the example above is 14, because the order of operations dictates that the expressions within the parentheses gets performed before the expressions on the outside. So 6 + 1 = 7, 7 x 2 = 14.

Loading...