C# Arithmetic Operators with Example – Learn Basic Math Operations
Hey! So, you’re learning about arithmetic operators in C#? Great choice! These operators are like the math tools you’ll use every day while coding. They help you perform basic math operations like adding, subtracting, multiplying, and more.
What Are Arithmetic Operators?
In C#, arithmetic operators are used to perform calculations on numbers. If you’ve used a calculator before, you’re probably already familiar with these operations. In programming, they’re used to handle numbers and values in a similar way.
Let’s go over each operator and see how it works.
1. Addition (+)
The +
operator is used to add two numbers together. It’s just like the “plus” button on your calculator. For example:
int sum = 5 + 3; // sum will be 8
It adds 5
and 3
to get 8
.
2. Subtraction (-)
Next up, we have the -
operator. It’s used to subtract one number from another. Just like the “minus” button on a calculator. Here’s an example:
int difference = 10 - 4; // difference will be 6
It subtracts 4
from 10
to give you 6
.
3. Multiplication (*)
The *
operator is used to multiply numbers. Think of it as the “times” button. Let’s see an example:
int product = 4 * 3; // product will be 12
It multiplies 4
by 3
to give you 12
.
4. Division (/)
Now, the /
operator is for dividing numbers. Just like the “divide” button on a calculator. For example:
int quotient = 20 / 4; // quotient will be 5
It divides 20
by 4
, and the result is 5
. Keep in mind, C# will perform integer division if both numbers are integers. So, if you divide 7 / 3
, you’ll get 2
because C# will ignore the decimal part.
But, if you want a more precise result (with decimals), you can use floating-point numbers (like float
or double
). Here’s an example:
double preciseDivision = 7.0 / 3.0; // preciseDivision will be 2.3333
5. Modulus (%)
The %
operator is super handy! It’s used to find the remainder of a division. For example:
int remainder = 7 % 3; // remainder will be 1
It divides 7
by 3
, but instead of giving the result of the division, it gives you the remainder. So in this case, 7 ÷ 3
leaves a remainder of 1
.
Arithmetic operators table
Operator | Description | Example | Output |
---|---|---|---|
+ (Addition) |
Adds two numbers | int sum = 5 + 3; |
8 |
- (Subtraction) |
Subtracts one number from another | int diff = 10 - 4; |
6 |
* (Multiplication) |
Multiplies two numbers | int product = 4 * 3; |
12 |
/ (Division) |
Divides one number by another | int quotient = 20 / 4; |
5 |
% (Modulus) |
Returns the remainder of division | int remainder = 7 % 3; |
1 |
C# Arithmetic operators with example
Now, let’s say we want to create a simple program that takes two numbers from the user and performs all arithmetic operations on them. We’ll then display the results.
using System;
class ArithmeticOperatorsDemo
{
static void Main()
{
// Ask the user to enter two numbers
Console.Write("Enter the first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
// Perform arithmetic operations
int addition = num1 + num2; // Adds the two numbers
int subtraction = num1 - num2; // Subtracts the second number from the first
int multiplication = num1 * num2;// Multiplies the numbers
int division = num1 / num2; // Divides first number by second (integer division)
int modulus = num1 % num2; // Finds the remainder
// Display results
Console.WriteLine("\nResults:");
Console.WriteLine($"Addition: {num1} + {num2} = {addition}");
Console.WriteLine($"Subtraction: {num1} - {num2} = {subtraction}");
Console.WriteLine($"Multiplication: {num1} * {num2} = {multiplication}");
Console.WriteLine($"Division: {num1} / {num2} = {division}");
Console.WriteLine($"Modulus (Remainder): {num1} % {num2} = {modulus}");
}
}
Example Output
Let’s say the user enters 15 and 4 as input. The output would be:
Enter the first number: 15
Enter the second number: 4
Results:
Addition: 15 + 4 = 19
Subtraction: 15 - 4 = 11
Multiplication: 15 * 4 = 60
Division: 15 / 4 = 3
Modulus (Remainder): 15 % 4 = 3
This program helps us understand how arithmetic operators work in C#. It’s simple, useful, and you’ll use these operators in almost every C# program you write.
Now, go ahead and try it yourself!
Final Thoughts
Arithmetic operators in C# are your go-to tools for basic math. They’re simple to use, and you’ll need them almost every time you write code. Once you get the hang of them, you’ll be solving problems faster than ever.
Next what?
Now that you’ve got a good grip on arithmetic operators, it’s time to move on. Next, you will learn about comparison operators—these help you compare values and make decisions in your code.