Arithmetic Operators
In this page:
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
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: