DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# BasicsTopic 14 of 55
←PreviousPrevNextNext→

C# Arithmetic Operators - Complete Guide

Introduction to Arithmetic Operators

Arithmetic operators in C# are used to perform mathematical operations on numeric values. These operators support addition, subtraction, multiplication, division, and other operations needed for calculations.

C# arithmetic operators work with numeric types such as integers, floating-point numbers, and decimals. A strong understanding of these operators is essential for implementing calculations in your programs.

Basic Arithmetic Operators

The following example demonstrates addition, subtraction, multiplication, division, modulus, and the difference between integer and floating-point division:

Example
using System;

namespace ArithmeticOperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 20;
            int b = 10;
            
            int sum = a + b;
            Console.WriteLine($"Addition: {a} + {b} = {sum}");
            
            int difference = a - b;
            Console.WriteLine($"Subtraction: {a} - {b} = {difference}");
            
            int product = a * b;
            Console.WriteLine($"Multiplication: {a} * {b} = {product}");
            
            int quotient = a / b;
            Console.WriteLine($"Division: {a} / {b} = {quotient}");
            
            int remainder = a % b;
            Console.WriteLine($"Modulus: {a} % {b} = {remainder}");
            
            double x = 20.0;
            double y = 3.0;
            double floatingQuotient = x / y;
            Console.WriteLine($"Floating-point division: {x} / {y} = {floatingQuotient}");
            
            int intResult = 7 / 2;          // integer division
            double doubleResult = 7.0 / 2;  // floating-point division
            Console.WriteLine($"Integer division 7/2 = {intResult}");
            Console.WriteLine($"Floating-point division 7.0/2 = {doubleResult}");
        }
    }
}
Output
Addition: 20 + 10 = 30
Subtraction: 20 - 10 = 10
Multiplication: 20 * 10 = 200
Division: 20 / 10 = 2
Modulus: 20 % 10 = 0
Floating-point division: 20 / 3 = 6.666666666666667
Integer division 7/2 = 3
Floating-point division 7.0/2 = 3.5

Increment and Decrement Operators

The increment operator (++) increases a variable’s value by 1, while the decrement operator (--) decreases it by 1. They can be used in prefix or postfix form:

Example
using System;

namespace IncrementDecrementExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5;
            
            Console.WriteLine($"Original x: {x}");
            Console.WriteLine($"x++: {x++}");
            Console.WriteLine($"After x++: {x}");
            
            Console.WriteLine($"++x: {++x}");
            Console.WriteLine($"After ++x: {x}");
            
            Console.WriteLine($"x--: {x--}");
            Console.WriteLine($"After x--: {x}");
            
            Console.WriteLine($"--x: {--x}");
            Console.WriteLine($"After --x: {x}");
            
            int counter = 0;
            Console.WriteLine($"\nCounter: {counter}");
            
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"Loop {i + 1}: counter = {counter++}");
            }
            
            Console.WriteLine($"Final counter: {counter}");
        }
    }
}
Output
Original x: 5
x++: 5
After x++: 6
++x: 7
After ++x: 7
x--: 7
After x--: 6
--x: 5
After --x: 5

Counter: 0
Loop 1: counter = 0
Loop 2: counter = 1
Loop 3: counter = 2
Loop 4: counter = 3
Loop 5: counter = 4
Final counter: 5

Operator Precedence and Associativity

Arithmetic operators follow precedence and associativity rules that control evaluation order. Multiplication, division, and modulus have higher precedence than addition and subtraction. Parentheses can be used to change the evaluation order explicitly.

Example
using System;

namespace OperatorPrecedenceExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int result1 = 2 + 3 * 4;      // 14
            int result2 = (2 + 3) * 4;    // 20
            
            Console.WriteLine($"2 + 3 * 4 = {result1}");
            Console.WriteLine($"(2 + 3) * 4 = {result2}");
            
            double complexResult = 10 + 2 * 3 - 4 / 2 + 5 % 3;
            Console.WriteLine($"10 + 2 * 3 - 4 / 2 + 5 % 3 = {complexResult}");
            
            double controlledResult = (10 + 2) * (3 - 4) / 2 + 5 % 3;
            Console.WriteLine($"(10 + 2) * (3 - 4) / 2 + 5 % 3 = {controlledResult}");
            
            double preciseCalculation = (10.0 + 2.5) * 3.0 / 4.0;
            Console.WriteLine($"(10.0 + 2.5) * 3.0 / 4.0 = {preciseCalculation}");
            
            int trickyResult = 3 / 2 * 4;  // 4 due to integer division
            Console.WriteLine($"3 / 2 * 4 = {trickyResult}");
            
            double correctResult = 3.0 / 2 * 4;  // 6.0
            Console.WriteLine($"3.0 / 2 * 4 = {correctResult}");
        }
    }
}
Output
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
10 + 2 * 3 - 4 / 2 + 5 % 3 = 16
(10 + 2) * (3 - 4) / 2 + 5 % 3 = -4
(10.0 + 2.5) * 3.0 / 4.0 = 9.375
3 / 2 * 4 = 4
3.0 / 2 * 4 = 6
Test your knowledge: C# Arithmetic Operators - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# BasicsTopic 14 of 55
←PreviousPrevNextNext→