C# Number Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, you can identify if a string is a number, extract a number from a string, check if the console input is a number, and check if an integer is a power of 2 using various methods. Here are some examples:
string input = "123"; bool isNumber = int.TryParse(input, out int result);
In this example, the int.TryParse
method is called with the input
variable as the first argument, and an out
variable result
of type int
as the second argument. The TryParse
method attempts to parse the input string as an integer and stores the result in the result
variable. If the parse operation is successful, the method returns true
, indicating that the input string is a number.
string input = "abc123def"; string numberString = new string(input.Where(char.IsDigit).ToArray()); int number = int.Parse(numberString);
In this example, the input
variable contains a string that contains a number. The Where
LINQ extension method is called on the input string to filter out all non-digit characters, and the resulting characters are converted to a new string using the new string
constructor. The resulting string is then parsed as an integer using the int.Parse
method.
Console.Write("Enter a number: "); string input = Console.ReadLine(); bool isNumber = int.TryParse(input, out int result); if (isNumber) { Console.WriteLine("Input is a number: " + result); } else { Console.WriteLine("Input is not a number"); }
In this example, the Console.ReadLine
method is called to read a string input from the console. The int.TryParse
method is then called with the input string as the first argument, and an out
variable result
of type int
as the second argument. The TryParse
method attempts to parse the input string as an integer and stores the result in the result
variable. If the parse operation is successful, the isNumber
variable is set to true
, and the result is printed to the console. If the parse operation fails, the isNumber
variable is set to false
, and a message indicating that the input is not a number is printed to the console.
int number = 8; bool isPowerOfTwo = (number != 0) && ((number & (number - 1)) == 0);
In this example, the number
variable contains an integer value. The isPowerOfTwo
variable is set to true
if the number
variable is not equal to zero and the bitwise AND operation between number
and number - 1
is equal to zero. This is because a power of 2 has only one bit set, and the bitwise AND of a power of 2 and the power of 2 minus 1 will always be zero.
C# check if a number is positive/negative
int number = 42; bool isPositive = (number > 0); bool isNegative = (number < 0);
Checking if a number is even or odd in C#
int number = 42; bool isEven = (number % 2 == 0); bool isOdd = (number % 2 != 0);
C# check if a number is prime
int number = 7; bool isPrime = Enumerable.Range(2, (int)Math.Sqrt(number) - 1).All(i => number % i > 0);
Validating integer range in C#
int number = 42; bool isInRange = (number >= minValue && number <= maxValue);
Detecting integer overflow in C#
checked { int result = int.MaxValue + 1; // Throws OverflowException }
C# check if a number is NaN or Infinity
double result = 0.0 / 0.0; bool isNaN = double.IsNaN(result); bool isInfinity = double.IsInfinity(result);
Checking for divisibility in C#
int number = 42; bool isDivisibleBy3 = (number % 3 == 0);
C# check if a number is a power of two
int number = 64; bool isPowerOfTwo = (number & (number - 1)) == 0;
Validating decimal precision in C#
decimal number = 123.456m; bool hasValidPrecision = (number % 1 == 0);
C# check if a number is a perfect square
int number = 16; double squareRoot = Math.Sqrt(number); bool isPerfectSquare = squareRoot % 1 == 0;
C# check if a number is within a tolerance
double expectedValue = 10.0; double actualValue = 10.00001; double tolerance = 0.001; bool isWithinTolerance = Math.Abs(expectedValue - actualValue) < tolerance;
C# check if a number is a Fibonacci number
int number = 21; bool isFibonacci = IsPerfectSquare(5 * number * number + 4) || IsPerfectSquare(5 * number * number - 4); static bool IsPerfectSquare(int x) { int sqrt = (int)Math.Sqrt(x); return sqrt * sqrt == x; }
C# check if a number is a multiple of another
int number = 15; int multipleOf = 3; bool isMultiple = (number % multipleOf == 0);
C# check if a number is a palindrome
int number = 1221; string numberString = number.ToString(); bool isPalindrome = numberString.SequenceEqual(numberString.Reverse());
Detecting numeric overflow in C#
Overflow can occur during arithmetic operations on numeric types, especially when using checked
keyword.
checked { int result = int.MaxValue * 2; // Throws OverflowException }