Arithmetic Operators
Arithmetic operators are the calculator buttons for adding, subtracting, multiplying, dividing, and finding what is left over. C# uses them to do math with your numbers.
In this page:
Syntax
int result = a + b;
int result = a - b;
int result = a * b;
int result = a / b; // integer division truncates
int result = a % b;
Arithmetic Operators
C# provides +, -, *, /, and % for basic arithmetic. Integer division truncates any fractional part, while dividing two doubles keeps precision. The % operator returns the remainder of a division.
Warning:
Dividing two integers, like
7 / 2, gives 3, not 3.5 — cast one operand to double to get a fractional result.Example: Arithmetic Operators
using System;
class Program
{
static void Main(string[] args)
{
int a = 7, b = 2;
Console.WriteLine(a + b);
Console.WriteLine(a - b);
Console.WriteLine(a * b);
Console.WriteLine(a / b); // integer division
Console.WriteLine(a % b); // remainder
Console.WriteLine((double)a / b); // fractional division
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Dividing two ints and expecting a decimal result, when
7 / 2is 3. - Dividing an integer by zero, which throws
DivideByZeroException. - Forgetting that
%returns a negative result for a negative left operand.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: