Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 870 of 2547
Find missing number in a sequence in C#
Finding missing numbers in a sequence is a common programming problem. In C#, you can solve this efficiently using LINQ operations by comparing the original sequence with a complete range of numbers. Using LINQ Except Method The most straightforward approach is to create a complete range from the minimum to maximum value and use the Except method to find missing numbers − using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List myList = new List(){1, 2, 3, 5, 8, ...
Read MoreDivision Operator in C#
The division operator (/) in C# is used to divide one number by another. It performs mathematical division between a numerator and denominator, such as 9 / 3 = 3. The division operator is part of the arithmetic operators in C# and behaves differently depending on the data types involved. Understanding these differences is crucial for accurate calculations. Syntax Following is the syntax for the division operator − result = numerator / denominator; Integer Division When both operands are integers, C# performs integer division, which truncates the decimal part and returns only ...
Read MoreHow to convert a Decimal to Octal using C#?
To convert a decimal number to its octal equivalent in C#, we use the division method where we repeatedly divide the decimal number by 8 and collect the remainders. The octal number system uses base 8, meaning it only uses digits 0-7. How It Works The conversion process involves dividing the decimal number by 8 repeatedly and storing the remainders in reverse order. Each remainder represents a digit in the octal representation. Decimal to Octal Conversion Process 18 ÷ 8 = ...
Read MoreHow to find a number using Pythagoras Theorem using C#?
The Pythagorean theorem states that in a right-angled triangle, the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. In C#, we can use the Math.Sqrt() method to calculate the hypotenuse or find missing sides. Syntax The basic formula for Pythagorean theorem is − c² = a² + b² c = Math.Sqrt(a * a + b * b) Where c is the hypotenuse, and a and b are the other two sides of the right triangle. ...
Read MoreHow to Convert Hex String to Hex Number in C#?
In C#, converting a hexadecimal string to its numeric value is a common operation. Hexadecimal strings represent numbers in base 16 using digits 0-9 and letters A-F. C# provides several methods to convert hex strings to different numeric types depending on your needs. Syntax Following is the syntax for converting hex strings using different methods − Convert.ToInt32(hexString, 16) Convert.ToSByte(hexString, 16) int.Parse(hexString, NumberStyles.HexNumber) Using Convert.ToInt32() Method The most commonly used method is Convert.ToInt32() which converts a hex string to a 32-bit signed integer − using System; public class Program { ...
Read MoreIntersect two lists in C#
In C#, you can find common elements between two lists using the Intersect() method from LINQ. This method returns an IEnumerable containing elements that appear in both lists, automatically removing duplicates. Syntax Following is the syntax for using the Intersect() method − IEnumerable result = list1.Intersect(list2); The method can also accept a custom equality comparer − IEnumerable result = list1.Intersect(list2, comparer); Using Intersect() with Integer Lists The most common use case is finding intersection between two integer lists − using System; using System.Collections.Generic; using System.Linq; class ...
Read MoreHow to convert a 2D array into 1D array in C#?
Converting a 2D array to a 1D array in C# involves flattening the multi-dimensional structure into a single-dimensional array. This process is commonly used in scenarios like matrix operations, data serialization, or when interfacing with APIs that expect 1D arrays. Using Nested Loops The most straightforward approach is using nested loops to iterate through the 2D array and copy elements to the 1D array − using System; class Program { static void Main(string[] args) { int[, ] a = new int[2, 2] {{1, ...
Read MoreHow to find a number in a string in C#?
To find a number in a string in C#, you can use Regular Expressions (Regex) from the System.Text.RegularExpressions namespace. The Regex pattern \d+ matches one or more consecutive digits in a string. Syntax Following is the syntax for creating a Regex pattern to match numbers − Regex regex = new Regex(@"\d+"); Match match = regex.Match(inputString); Following is the syntax for checking if a match was found − if (match.Success) { string number = match.Value; } Using Regex to Find the First Number The Match method ...
Read MoreC# program to check whether a given string is Heterogram or not
A Heterogram is a string that contains no duplicate letters. Each character in the string appears exactly once. For example, words like "mobile", "cry", and "laptop" are heterograms because no letter is repeated. Examples of Heterograms mobile - No repeated letters cry - All unique letters laptop - Each letter appears once Algorithm The algorithm uses an integer array to track which letters have been encountered. For each character in the string − Convert the character to an array index using str[i] - ...
Read MoreConvert.ToInt16 Method in C#
The Convert.ToInt16 method in C# converts a specified value to a 16-bit signed integer (short). This method accepts various data types including double, string, boolean, and other numeric types, and returns a short value ranging from -32, 768 to 32, 767. When converting from floating-point numbers, the method performs rounding to the nearest integer. If the value is exactly halfway between two integers, it rounds to the even number. Syntax Following are the common overloads of the Convert.ToInt16 method − public static short ToInt16(double value); public static short ToInt16(string value); public static short ToInt16(bool value); ...
Read More