C# Arithmetic Operators

In this chapter you will learn:
  • What is operator in C#?
  • What is Arithmetic Operator?
  • How to use Arithmetic operator in program?
  • What is Escape Character and how to use it?

Operators are a very useful thing in computing value. While writing a program, you need various types of operators to calculate the value. These operators are categorized as different categories in C sharp tutorial that performs specific task ex. The C# arithmetic operator performs the basic calculation as add, subtraction, multiplication, division, and modulus whereas other operators perform a different kind of task. You will learn one by one all these operators in few next chapters.

Arithmetic Operators are used for basic mathematical calculation in C# programming. The list of Arithmetic Operators is given below:

Arithmetic Operators:

Operator Description Examples
+ Add numbers X=num1+num2
- Subtract numbers X=num1-num2
* Multiply numbers X=num1*num2
/ Divide numbers X=num1/num2
% Divide two numbers and returns reminder X=22%10 then X will be X=2
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Arithmetic_Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2;
            int add, sub, mul;
            float div;

            //Accepting Values from users
            Console.Write("Enter first number\t\t");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("\n\nEnter second number\t\t");
            num2 = Convert.ToInt32(Console.ReadLine());

            //Processing Values
            //Used + operator for adding values
            add = num1 + num2;
            //Used - operator for subtracting values
            sub = num1 - num2;
            //Used * operator for multiplying values
            mul = num1 * num2;
            //Used / operator for dividing values
            div = (float)num1 / num2;

            //Displaying Output
            Console.WriteLine("\n\n=====================\n");
            Console.WriteLine("Addition\t\t{0}", add);
            Console.WriteLine("Subtraction\t\t{0}", sub);
            Console.WriteLine("Multiplication\t\t{0}", mul);
            Console.WriteLine("Division\t\t{0}", div);
            Console.WriteLine("\n=======================\n");

            Console.ReadLine();
        }
    }
}

Output

Enter first number   15
Enter second number   12
=====================
Addition   27
Subtraction   3
Multiplication   180
Division   1.25
=======================
__

Note: \t,\n these are the special characters in C# that is known as Escape Characters. It is used for special purpose such as inserting line break, backspace etc.

 

List of Escape Characters:

Escape Characters Description
\’ Used for single quotation mark
\" Used for double quotation mark
\\ Used for backslash
\0 Null
\a Audible alert
\b Used for backspace
\f Used for form feed
\n Used for line break
\r Used for Carriage return
\t Used for horizontal tab
\v Used for vertical tab

Summary

In this chapter, you learned about arithmetic operators in C#. You also learned how to use arithmetic operator in program. Escape characters are provides more functionality in a program so, you learned about different type escape characters in C#. In next chapter you will learn about assignment operators in C sharp.
 

Share your thought