Home  >  Article  >  Backend Development  >  Numbers in C#

Numbers in C#

WBOY
WBOYforward
2023-09-15 23:13:021327browse

C# 中的数字

For numbers in C#, use the int type. It represents an integer, which can be positive or negative.

Let us see how to use mathematical operators in C# - Add two integers

using System;
using System.Linq;

class Program {
   static void Main() {
      int x = 20;
      int y = 30;
      int sum = 0;

      sum = x + y;
      Console.WriteLine(sum);
   }
}

Now let us understand the order of these mathematical operators i.e. operator precedence.

Operator precedence determines the grouping of terms in an expression. This affects the evaluation of expressions. Some operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.

For example x = 9 2 * 5; here, x is assigned as 19 instead of 55 because the operator * has higher precedence, so 2*5 is calculated first and then 9 is added into it.

The following example shows the order of operators -

Example

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {

         int a = 200;
         int b = 100;
         int c = 150;
         int d = 50;
         int res;

         res = (a + b) * c / d;
         Console.WriteLine("Value of (a + b) * c / d is : {0}", res);

         res = ((a + b) * c) / d;
         Console.WriteLine("Value of ((a + b) * c) / d is : {0}", res);

         res = (a + b) * (c / d);
         Console.WriteLine("Value of (a + b) * (c / d) : {0}",res);

         res = a + (b * c) / d;
         Console.WriteLine("Value of a + (b * c) / d : {0}",res);

         Console.ReadLine();
      }
   }
}

The above is the detailed content of Numbers in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete