Numbers in C#

Numbers in C# are represented by various data types, with int being the most commonly used for whole numbers. The int type represents a 32-bit signed integer that can store values from -2,147,483,648 to 2,147,483,647.

Basic Integer Operations

C# supports standard mathematical operations on integers using arithmetic operators. Here's how to perform basic addition −

using System;

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

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

The output of the above code is −

Sum: 50

Numeric Data Types in C#

C# provides several numeric data types to handle different ranges and precision requirements −

Data Type Size Range
byte 8-bit 0 to 255
short 16-bit -32,768 to 32,767
int 32-bit -2,147,483,648 to 2,147,483,647
long 64-bit -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 32-bit ±1.5 x 10^-45 to ±3.4 x 10^38
double 64-bit ±5.0 × 10^-324 to ±1.7 × 10^308

Operator Precedence

Operator precedence determines the order in which operations are performed in mathematical expressions. Multiplication and division have higher precedence than addition and subtraction, similar to standard mathematical rules.

Operator Precedence (Highest to Lowest) ( ) Parentheses - Highest Priority * / % Multiplication, Division, Modulus + - Addition, Subtraction - Lowest Priority Example: 9 + 2 * 5 = 9 + 10 = 19 (not 55)

Example

using System;

class Program {
   static void Main() {
      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);
   }
}

The output of the above code is −

Value of (a + b) * c / d is : 900
Value of ((a + b) * c) / d is : 900
Value of (a + b) * (c / d) : 900
Value of a + (b * c) / d : 500

Working with Different Numeric Types

Example

using System;

class Program {
   static void Main() {
      byte smallNumber = 255;
      short mediumNumber = 32000;
      int largeNumber = 2000000;
      long veryLargeNumber = 9000000000L;
      
      float decimalNumber = 3.14f;
      double preciseDecimal = 3.141592653589793;

      Console.WriteLine("Byte: " + smallNumber);
      Console.WriteLine("Short: " + mediumNumber);
      Console.WriteLine("Int: " + largeNumber);
      Console.WriteLine("Long: " + veryLargeNumber);
      Console.WriteLine("Float: " + decimalNumber);
      Console.WriteLine("Double: " + preciseDecimal);
   }
}

The output of the above code is −

Byte: 255
Short: 32000
Int: 2000000
Long: 9000000000
Float: 3.14
Double: 3.141592653589793

Conclusion

C# provides various numeric data types to handle different ranges and precision requirements. Understanding operator precedence is crucial for writing correct mathematical expressions, with parentheses having the highest precedence, followed by multiplication/division, and finally addition/subtraction.

Updated on: 2026-03-17T07:04:35+05:30

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements