Numbers in C#

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-09-15 23:13:02
forward
1406 people have browsed it

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);
   }
}
Copy after login

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();
      }
   }
}
Copy after login

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

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template