C# Arithmetic Operators - Complete Guide
Introduction to Arithmetic Operators
Arithmetic operators in C# are used to perform mathematical operations on numeric values. These operators support addition, subtraction, multiplication, division, and other operations needed for calculations.
C# arithmetic operators work with numeric types such as integers, floating-point numbers, and decimals. A strong understanding of these operators is essential for implementing calculations in your programs.
Basic Arithmetic Operators
The following example demonstrates addition, subtraction, multiplication, division, modulus, and the difference between integer and floating-point division:
using System;
namespace ArithmeticOperatorsExample
{
class Program
{
static void Main(string[] args)
{
int a = 20;
int b = 10;
int sum = a + b;
Console.WriteLine($"Addition: {a} + {b} = {sum}");
int difference = a - b;
Console.WriteLine($"Subtraction: {a} - {b} = {difference}");
int product = a * b;
Console.WriteLine($"Multiplication: {a} * {b} = {product}");
int quotient = a / b;
Console.WriteLine($"Division: {a} / {b} = {quotient}");
int remainder = a % b;
Console.WriteLine($"Modulus: {a} % {b} = {remainder}");
double x = 20.0;
double y = 3.0;
double floatingQuotient = x / y;
Console.WriteLine($"Floating-point division: {x} / {y} = {floatingQuotient}");
int intResult = 7 / 2; // integer division
double doubleResult = 7.0 / 2; // floating-point division
Console.WriteLine($"Integer division 7/2 = {intResult}");
Console.WriteLine($"Floating-point division 7.0/2 = {doubleResult}");
}
}
}
Addition: 20 + 10 = 30 Subtraction: 20 - 10 = 10 Multiplication: 20 * 10 = 200 Division: 20 / 10 = 2 Modulus: 20 % 10 = 0 Floating-point division: 20 / 3 = 6.666666666666667 Integer division 7/2 = 3 Floating-point division 7.0/2 = 3.5
Increment and Decrement Operators
The increment operator (++) increases a variable’s value by 1, while the decrement operator (--) decreases it by 1. They can be used in prefix or postfix form:
using System;
namespace IncrementDecrementExample
{
class Program
{
static void Main(string[] args)
{
int x = 5;
Console.WriteLine($"Original x: {x}");
Console.WriteLine($"x++: {x++}");
Console.WriteLine($"After x++: {x}");
Console.WriteLine($"++x: {++x}");
Console.WriteLine($"After ++x: {x}");
Console.WriteLine($"x--: {x--}");
Console.WriteLine($"After x--: {x}");
Console.WriteLine($"--x: {--x}");
Console.WriteLine($"After --x: {x}");
int counter = 0;
Console.WriteLine($"\nCounter: {counter}");
for (int i = 0; i < 5; i++)
{
Console.WriteLine($"Loop {i + 1}: counter = {counter++}");
}
Console.WriteLine($"Final counter: {counter}");
}
}
}
Original x: 5 x++: 5 After x++: 6 ++x: 7 After ++x: 7 x--: 7 After x--: 6 --x: 5 After --x: 5 Counter: 0 Loop 1: counter = 0 Loop 2: counter = 1 Loop 3: counter = 2 Loop 4: counter = 3 Loop 5: counter = 4 Final counter: 5
Operator Precedence and Associativity
Arithmetic operators follow precedence and associativity rules that control evaluation order. Multiplication, division, and modulus have higher precedence than addition and subtraction. Parentheses can be used to change the evaluation order explicitly.
using System;
namespace OperatorPrecedenceExample
{
class Program
{
static void Main(string[] args)
{
int result1 = 2 + 3 * 4; // 14
int result2 = (2 + 3) * 4; // 20
Console.WriteLine($"2 + 3 * 4 = {result1}");
Console.WriteLine($"(2 + 3) * 4 = {result2}");
double complexResult = 10 + 2 * 3 - 4 / 2 + 5 % 3;
Console.WriteLine($"10 + 2 * 3 - 4 / 2 + 5 % 3 = {complexResult}");
double controlledResult = (10 + 2) * (3 - 4) / 2 + 5 % 3;
Console.WriteLine($"(10 + 2) * (3 - 4) / 2 + 5 % 3 = {controlledResult}");
double preciseCalculation = (10.0 + 2.5) * 3.0 / 4.0;
Console.WriteLine($"(10.0 + 2.5) * 3.0 / 4.0 = {preciseCalculation}");
int trickyResult = 3 / 2 * 4; // 4 due to integer division
Console.WriteLine($"3 / 2 * 4 = {trickyResult}");
double correctResult = 3.0 / 2 * 4; // 6.0
Console.WriteLine($"3.0 / 2 * 4 = {correctResult}");
}
}
}
2 + 3 * 4 = 14 (2 + 3) * 4 = 20 10 + 2 * 3 - 4 / 2 + 5 % 3 = 16 (10 + 2) * (3 - 4) / 2 + 5 % 3 = -4 (10.0 + 2.5) * 3.0 / 4.0 = 9.375 3 / 2 * 4 = 4 3.0 / 2 * 4 = 6