Arithmetic Operators In C#

In this article explain I will explain how to perform the various arithmetic operations like Addition, Subtraction, Multiplication and divide In C#.
  • 2235

Introduction

The concept of arithmetic operators are very useful in C#. Arithmetic Operators are used for basic mathematical calculation in C# programming. When we are writing a program, you need various types of operators to calculate value. These operator performs basic calculation such as add, subtraction, multiplication, division, and modulus. The list of Arithmetic Operators is given below

Arithmetic Operators

Operator Explanation Example
+ Add numbers A = num1 + num2
- Subtract numbers A = num1 - num2
* Multiply numbers A = num1 *  num2
/ Divide numbers A = num1 / num2
% Divide two numbers and returns reminder A = 10%2 then A will be 2

Example

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;

            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());

            // "+" operator for adding values

            add = num1 + num2;

            //Used - operator for subtracting values

            sub = num1 - num2;

            // "*" operator for multiplying values

            mul = num1 * num2;

            // "/" 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();

        }

    }

}

 

 The output of following program

 Clipboard176.jpg

© 2013 dotNetheaven. All rights reserved.