C# - Arithmetic Operators



What Are Arithmetic Operators in C#?

C# arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, division, and modulus between numeric data types. These operators are binary operators, which need two operands to perform the operations.

List of C# Arithmetic Operators

Following table shows all the arithmetic operators supported by C#. Assume variable A holds 10 and variable B holds 20, then −

Operator Symbol Description Example
Addition + Adds two operands A + B = 30
Subtraction - Subtracts second operand from the first A - B = -10
Multiplication * Multiplies both operands A * B = 200
Division / Divides numerator by de-numerator B / A = 2
Modulus % Modulus Operator and remainder of after an integer division B % A = 0
Increment Operator ++ Increment operator increases integer value by one A++ = 11
Decrement Operator -- Decrement operator decreases integer value by one A-- = 9

Example of Arithmetic Operators

The following example demonstrates all the arithmetic operators available in C# −

using System;

namespace OperatorsAppl {
   class Program { 
      static void Main(string[] args) { 
         int a = 21;
         int b = 10;
         int c;

         c = a + b;
         Console.WriteLine("Line 1 - Value of c is {0}", c);
         
         c = a - b;
         Console.WriteLine("Line 2 - Value of c is {0}", c);
         
         c = a * b;
         Console.WriteLine("Line 3 - Value of c is {0}", c);
         
         c = a / b;
         Console.WriteLine("Line 4 - Value of c is {0}", c);
         
         c = a % b;
         Console.WriteLine("Line 5 - Value of c is {0}", c);
         
         c = a++;
         Console.WriteLine("Line 6 - Value of c is {0}", c);
         
         c = a--;
         Console.WriteLine("Line 7 - Value of c is {0}", c);
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result −

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22

More Examples of Arithmetic Operators

Example 1: Basic Arithmetic Operations

In this example, we are performing basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus.

using System;

class Program
{
    static void Main()
    {
        int a = 10, b = 3;

        Console.WriteLine("Addition: " + (a + b));      
        Console.WriteLine("Subtraction: " + (a - b));    
        Console.WriteLine("Multiplication: " + (a * b)); 
        Console.WriteLine("Division: " + (a / b));       
        Console.WriteLine("Modulus: " + (a % b));        
    }
}

In this example, we are checking whether it is not raining to decide if we should go outside.

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1

Example 2: Integer vs. Floating-Point Division

In this example, we are comparing integer division with floating-point division to understand the difference.

using System;

class Program
{
    static void Main()
    {
        int a = 7, b = 2;
        double c = 7, d = 2;

        Console.WriteLine("Integer Division: " + (a / b));
        Console.WriteLine("Floating-Point Division: " + (c / d));
    }
}

In this example, we are checking whether it is not raining to decide if we should go outside.

Integer Division: 3
Floating-Point Division: 3.5

Use Cases

The following are the real-world use cases of arithmetic operators in C#:

Example 3: Calculating the Area of a Circle

In this example, we are calculating the area of a circle using the formula r.

using System;

class Program
{
    static void Main()
    {
        const double Pi = 3.14159;
        double radius = 5.5;
        double area = Pi * radius * radius;

        Console.WriteLine("Circle Area: " + area);
    }
}

In this example, we are checking whether it is not raining to decide if we should go outside.

Circle Area: 95.0330975

Example 4: Converting Temperature (Celsius to Fahrenheit)

In this example, we are converting a temperature value from Celsius to Fahrenheit.

using System;

class Program
{
    static void Main()
    {
        double celsius = 30;
        double fahrenheit = (celsius * 9 / 5) + 32;

        Console.WriteLine("Temperature in Fahrenheit: " + fahrenheit);
    }
}

In this example, we are checking whether it is not raining to decide if we should go outside.

Temperature in Fahrenheit: 86

Example 5: Calculating Monthly Loan Payment

In this example, we are calculating the monthly loan payment using a standard loan payment formula.

using System;

class Program
{
    static void Main()
    {
        double principal = 10000; // Loan amount
        double rate = 5.5 / 100 / 12; // Monthly interest rate
        int months = 60; // Loan term

        double monthlyPayment = (principal * rate) / (1 - Math.Pow(1 + rate, -months));

        Console.WriteLine("Monthly Loan Payment: " + monthlyPayment);
    }
}

In this example, we are checking whether it is not raining to decide if we should go outside.

Monthly Loan Payment: 190.34

Best Practices

The following are the best practices while working with numerical calculations.

  • You should use double or decimal for precision-sensitive calculations like finance and engineering.
  • You must check for division by zero before performing a division.
  • You should use math functions like Math.Pow and Math.Sqrt for complex calculations.
  • You should use const for fixed values like Pi = 3.14159.

Frequently Asked Questions

Q1: What happens if I divide by zero in C#?

  • If you're using int, you'll get a DivideByZeroException.
  • If you're using double, it just returns Infinity.

Q2: Whats the difference between / and %?

  • / gives you the result of division (e.g., 10 / 3 gives 3).
  • % gives you the remainder (e.g., 10 % 3 gives 1).

Q3: Can I use Math functions with arithmetic operators?

Yes! You can use:

  • Math.Sqrt(x) for square roots.
  • Math.Pow(a, b) for exponents.
  • Math.Abs(x) for absolute values.
Advertisements