➕ C# Arithmetic Operators – Perform Mathematical Operations in C#
🧲 Introduction – Why Use Arithmetic Operators in C#?
Arithmetic operators allow you to perform basic mathematical calculations in C#. These operators are essential for any task involving numbers — whether it’s summing prices, calculating averages, or applying formulas.
🎯 In this guide, you’ll learn:
- The full list of arithmetic operators in C#
- Syntax and usage of each operator
- Code examples with real output
- Best practices and common pitfalls
🔍 Core Concept – What Are Arithmetic Operators?
Arithmetic operators in C# are symbols that perform mathematical operations on numeric types like int
, float
, double
, and decimal
.
🔹 List of Arithmetic Operators:
Operator | Symbol | Description | Example | Result |
---|---|---|---|---|
Addition | + | Adds two values | 5 + 3 | 8 |
Subtraction | - | Subtracts one from another | 5 - 3 | 2 |
Multiplication | * | Multiplies two values | 5 * 3 | 15 |
Division | / | Divides one by another | 6 / 3 | 2 |
Modulus | % | Returns the remainder | 5 % 3 | 2 |
Unary Plus | +x | Positive sign (usually implicit) | +5 | 5 |
Unary Minus | -x | Negates a value | -5 | -5 |
💻 Code Example – Arithmetic in Action
using System;
class ArithmeticDemo
{
static void Main()
{
int a = 10;
int b = 3;
Console.WriteLine($"Addition: {a + b}");
Console.WriteLine($"Subtraction: {a - b}");
Console.WriteLine($"Multiplication: {a * b}");
Console.WriteLine($"Division: {a / b}");
Console.WriteLine($"Modulus: {a % b}");
}
}
📤 Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
🧮 Data Type Compatibility
Data Type | Supported Arithmetic Ops |
---|---|
int | All arithmetic ops |
double | All, including decimals |
decimal | Precise finance/math use |
byte , short | Requires type casting |
⚠️ Note: Integer division (int / int
) drops the decimal. Use double
or float
for decimal results.
🔁 Compound Assignments
You can combine arithmetic with assignment:
int x = 10;
x += 5; // Same as x = x + 5;
x *= 2; // Same as x = x * 2;
📘 Best Practice: Use compound assignment for cleaner, more concise code.
💡 Tips, Pitfalls & Best Practices
💡 Tip: Use decimal
for financial calculations to avoid floating-point precision errors.
⚠️ Pitfall: Dividing integers truncates decimals — use double
to preserve fractions.
📘 Best Practice: Always validate input before performing arithmetic to avoid divide-by-zero exceptions.
🛠️ Use Cases – Where Arithmetic Is Used
- Calculating totals, discounts, or interest
- Performing scientific or statistical computations
- Loop counters and mathematical formulas
- Processing sensor or numeric input data
📌 Summary – Recap & Next Steps
Arithmetic operators are at the core of mathematical operations in C#. They apply to nearly every application involving numbers and can be combined for more complex expressions.
🔍 Key Takeaways:
- C# supports addition, subtraction, multiplication, division, and modulus
- Use
double
ordecimal
for fractional and precise calculations - Watch out for integer division and divide-by-zero errors
⚙️ Coming up: Learn about ➕ C# Assignment Operators for updating variable values efficiently.
❓ FAQ – C# Arithmetic Operators
❓ What is the modulus operator used for in C#?
✅ The %
operator returns the remainder of a division, e.g., 10 % 3
returns 1
.
❓ What happens when dividing two integers in C#?
✅ The result is truncated to an integer (e.g., 5 / 2
= 2
). Use 5.0 / 2
for 2.5
.
❓ Can I use arithmetic with decimals or floats?
✅ Yes, but be cautious of rounding issues with float
and double
. Prefer decimal
for accuracy.
❓ What is a unary minus operator?
✅ It negates a number, e.g., -5
is the unary minus of 5
.
❓ Are arithmetic operators overloaded in C#?
✅ Yes. You can overload arithmetic operators for custom types using operator overloading.
Share Now :