C# Academy · Lesson

Arithmetic and Assignment Operators

Use +, -, *, /, % and compound assignment forms.

Lesson 1 of 413 steps

Arithmetic and Assignment Operators is a free C# Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

Arithmetic Operators

C# gives you the classic math operators: + add, - subtract, * multiply, / divide, and % remainder (modulo).

They work on numeric types like int, long, double, and decimal.

using System;

class Program {
    static void Main() {
        int a = 10, b = 3;
        Console.WriteLine(a + b);
        Console.WriteLine(a - b);
        Console.WriteLine(a * b);
    }
}

Integer Division

When both operands are integers, / performs integer division and truncates the fractional part.

10 / 3 is 3, not 3.33.

using System;

class Program {
    static void Main() {
        int result = 10 / 3;
        Console.WriteLine(result);
        Console.WriteLine(7 / 2);
    }
}

Floating-Point Division

To keep the fraction, make at least one operand a floating-point type, such as double. You can add .0 or cast.

using System;

class Program {
    static void Main() {
        double r1 = 10.0 / 3;
        Console.WriteLine(r1);
        double r2 = (double)7 / 2;
        Console.WriteLine(r2);
    }
}

The Modulo Operator

The % operator returns the remainder after division. It is great for checking even/odd or cycling values.

using System;

class Program {
    static void Main() {
        Console.WriteLine(10 % 3);
        Console.WriteLine(8 % 2);
        Console.WriteLine(7 % 4);
    }
}

Even or Odd with Modulo

A number is even when n % 2 == 0. This is the most common use of modulo.

using System;

class Program {
    static void Main() {
        for (int n = 1; n <= 5; n++) {
            string kind = (n % 2 == 0) ? "even" : "odd";
            Console.WriteLine(n + " is " + kind);
        }
    }
}

Operator Order in Expressions

C# follows math precedence: *, /, and % happen before + and -. Use parentheses to be explicit.

using System;

class Program {
    static void Main() {
        Console.WriteLine(2 + 3 * 4);
        Console.WriteLine((2 + 3) * 4);
    }
}

Compound Assignment: +=

Compound assignment combines an operation with assignment. x += 5 is shorthand for x = x + 5.

using System;

class Program {
    static void Main() {
        int score = 10;
        score += 5;
        Console.WriteLine(score);
        score += 20;
        Console.WriteLine(score);
    }
}

Compound Assignment: -= and *=

The same pattern works for every arithmetic operator. x -= 2 subtracts, x *= 3 multiplies in place.

using System;

class Program {
    static void Main() {
        int x = 20;
        x -= 5;
        Console.WriteLine(x);
        x *= 2;
        Console.WriteLine(x);
    }
}

Compound Assignment: /= and %=

/= divides and assigns, %= takes the remainder and assigns. They follow the same integer/floating rules as the base operators.

using System;

class Program {
    static void Main() {
        int y = 100;
        y /= 4;
        Console.WriteLine(y);
        y %= 7;
        Console.WriteLine(y);
    }
}

Increment and Decrement

++ adds one, -- subtracts one. Prefix (++x) changes the value before use; postfix (x++) after use.

using System;

class Program {
    static void Main() {
        int i = 5;
        Console.WriteLine(i++);
        Console.WriteLine(i);
        Console.WriteLine(++i);
    }
}

Building a Running Total

Compound assignment shines in loops where you accumulate a value.

using System;

class Program {
    static void Main() {
        int total = 0;
        for (int n = 1; n <= 5; n++) {
            total += n;
        }
        Console.WriteLine("Sum 1..5 = " + total);
    }
}

Quick Check

Test your understanding of arithmetic and compound assignment.

Recap

You learned the C# arithmetic operators + - * / %, that integer division truncates while floating-point division keeps the fraction, and that modulo gives the remainder.

Compound assignment (+= -= *= /= %=) updates a variable in place, and ++/-- step by one. These tools power counters and running totals.

Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “Arithmetic and Assignment Operators” lesson free?

Yes — the full text of “Arithmetic and Assignment Operators” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.

What will I learn in “Arithmetic and Assignment Operators”?

Use +, -, *, /, % and compound assignment forms. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Arithmetic and Assignment Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Assignment Operators
  2. Comparison and Logical Operators
  3. Bitwise and Shift Operators
  4. Operator Precedence and Associativity
← Back to C# Academy