🔷 C#

Arithmetic operations — C#

What is it and why?

Imagine you're playing a computer game. You have 5 lives. You complete a level and find a medkit that gives +3 lives. How many lives do you have now? That's right, 8. This is an arithmetic operation — addition.

In programming, as in life, we constantly need to calculate something: how many points a player earned, how much money is left in the account, how many days until New Year. Arithmetic operations are basic mathematical actions (addition, subtraction, multiplication, division) that a computer can perform instantly.

Why do you need to know this? If you want to create games, calculators, expense tracking programs, or even simple chatbots — you will constantly have to use arithmetic. A computer without it is like a calculator without buttons: a useless piece of metal.

Syntax and Code Examples

In C#, arithmetic operations look almost the same as in mathematics. Here are the basic symbols:

  • + — addition
  • - — subtraction
  • * — multiplication (asterisk, not a cross)
  • / — division (slash)
  • % — remainder of division (not clear yet? We'll figure it out soon)

Let's look at examples. Imagine we are writing a program to calculate a student's pocket money.

// Example 1: Adding money
int pocketMoney = 500; // Petya had 500 rubles
int giftFromGrandma = 200; // Grandma gave another 200
int totalMoney = pocketMoney + giftFromGrandma; // Adding
Console.WriteLine("Petya now has: " + totalMoney + " rubles");
// Output: Petya now has: 700 rubles

Here we created two variables (memory cells) with numbers, added them, and wrote the result into a third variable. Everything is like in mathematics: 500 + 200 = 700.

// Example 2: Subtracting expenses
int money = 1000; // Had a thousand
int iceCreamPrice = 150; // Ice cream costs 150
int remaining = money - iceCreamPrice; // Subtracting
Console.WriteLine("Remaining: " + remaining + " rubles");
// Output: Remaining: 850 rubles

Here we subtract the price of the ice cream from the available money. The computer does this in a microsecond.

// Example 3: Multiplying (how much money in a week?)
int dailyAllowance = 100; // 100 rubles per day
int daysInWeek = 7;
int weeklyTotal = dailyAllowance * daysInWeek; // Multiplying
Console.WriteLine("Accumulated in a week: " + weeklyTotal + " rubles");
// Output: Accumulated in a week: 700 rubles

Multiplication is just repeated addition. Instead of writing 100+100+100+100+100+100+100, we write 100 * 7. Convenient, isn't it?

// Example 4: Dividing (how many for each friend?)
int totalCandies = 20; // 20 candies
int friends = 4; // 4 friends
int candiesPerFriend = totalCandies / friends; // Dividing
Console.WriteLine("Each friend gets: " + candiesPerFriend + " candies");
// Output: Each friend gets: 5 candies

Division works as usual, but there's a trick: if you divide integers, the result will also be an integer. 20 / 4 = 5, that's fine. But what about 20 / 3? It will be 6, not 6.666... Because we are working with integers (int). For fractional numbers, use double or float.

// Example 5: Remainder of division (who is left out?)
int totalCandies = 20;
int friends = 3;
int remainder = totalCandies % friends; // % is the remainder
Console.WriteLine("Leftover candies: " + remainder + " candies");
// Output: Leftover candies: 2 candies

The % operator (percent sign) is actually not about percentages, but the remainder of division. 20 divided by 3 gives 6 whole and 2 remainder. This operator is very useful when you need to check if a number is even or odd (if number % 2 == 0, then it's even).

Comparison with Python

Python is another popular programming language. In it, arithmetic looks very similar, but there are a couple of differences. Let's compare:

Operation C# Python
Addition int a = 5 + 3; a = 5 + 3
Subtraction int b = 10 - 4; b = 10 - 4
Multiplication int c = 6 * 7; c = 6 * 7
Division double d = 10.0 / 3; (need a decimal point) d = 10 / 3 (in Python 3 gives 3.333...)
Remainder int e = 10 % 3; e = 10 % 3
Exponentiation double f = Math.Pow(2, 3); (via Math) f = 2 ** 3 (simply **)

Main differences:

  • In C#, you need to specify the variable type (int, double); in Python, you don't.
  • In C#, dividing integers gives an integer result (10 / 3 = 3); in Python 3, it gives a fractional result (10 / 3 = 3.333...).
  • In C#, for exponentiation you need to call a special function Math.Pow; in Python, there is a simple operator **.
  • In C#, each command ends with a semicolon (;); in Python, it doesn't.

Example in Python for comparison:

# Python version
pocket_money = 500
gift = 200
total = pocket_money + gift
print("Petya now has:", total, "rubles")
# Output: Petya now has: 700 rubles

As you can see, the logic is the same, but the syntax is simpler (no int, no ;). However, C# gives more control over data types, which helps avoid errors in large programs.

Typical Beginner Mistakes

When you're just starting out, mistakes are normal. Here are the most common pitfalls:

  1. Forgot the semicolon
    int x = 5 — error! Need: