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 847 of 2547
C# Program to check if a number is Positive, Negative, Odd, Even, Zero
In C#, you can determine if a number is positive, negative, odd, even, or zero using simple conditional statements and arithmetic operations. This involves checking the sign of the number and using the modulus operator to determine if a number is divisible by 2. Logic for Number Classification To classify a number, we use the following approaches − Positive/Negative/Zero: Compare the number with zero using relational operators. Odd/Even: Use the modulus operator (%) to check if the remainder when divided by 2 is zero. Number Classification Logic ...
Read MoreManipulate decimals with numeric operators in C#
The decimal data type in C# provides precise arithmetic operations for financial and monetary calculations. You can manipulate decimals using standard numeric operators such as +, -, *, /, and %. Decimal literals in C# must be suffixed with M or m to distinguish them from double values. This ensures precision is maintained during calculations. Syntax Following is the syntax for declaring decimal variables − decimal variableName = value M; Following is the syntax for basic arithmetic operations with decimals − decimal result = decimal1 + decimal2; // Addition decimal ...
Read MoreC# Decimal ("D") Format Specifier
The "D" (decimal) format specifier in C# is used to format integer types as a string of decimal digits (0-9). It displays the number with optional zero-padding to achieve a specified minimum length. Syntax Following is the syntax for the "D" format specifier − number.ToString("D") // Basic decimal format number.ToString("Dn") // Decimal with minimum n digits Where n is the minimum number of digits. If the number has fewer digits, it will be padded with leading zeros. Parameters ...
Read MoreC# program to display factors of entered number
A factor of a number is any integer that divides the number evenly without leaving a remainder. For example, the factors of 12 are 1, 2, 3, 4, 6, and 12 because each of these numbers divides 12 exactly. To find factors of a number in C#, we use the modulus operator (%) to check if the remainder is zero when dividing the number by potential factors. Algorithm The algorithm to find factors involves the following steps − Start with 1 and iterate through all numbers up to the given number For each number, check ...
Read MoreWhat is the difference between a float, double and a decimal in C#?
Float, double, and decimal are all value types in C# that represent numeric data with fractional parts. Each type differs in precision, memory size, and intended use cases. Understanding these differences is crucial for choosing the appropriate type for your specific needs. Syntax Following is the syntax for declaring float, double, and decimal variables − float floatValue = 3.14f; // 'f' suffix required double doubleValue = 3.14; // default for literals decimal decimalValue = 3.14m; // 'm' suffix required Float Value Type Float ...
Read MoreWhat is the difference between overriding and hiding in C#?
In C#, method overriding and method hiding (also called shadowing) are two different mechanisms for redefining parent class methods in a derived class. Method overriding uses the override keyword and provides polymorphic behavior, while method hiding uses the new keyword and creates a separate method that shadows the parent method. Syntax Following is the syntax for method overriding using the override keyword − public class BaseClass { public virtual void Method() { } } public class DerivedClass : BaseClass { public override void Method() { } } ...
Read MoreRecommended IDEs for C# on Windows/Linux/Mac OS
Choosing the right IDE is crucial for productive C# development. While Microsoft Visual Studio remains the gold standard on Windows, developers have excellent options across all major operating systems including Windows, Linux, and macOS. Microsoft Visual Studio (Windows) Visual Studio is the flagship IDE for C# development on Windows, offering comprehensive tools for building desktop, web, and mobile applications. Visual Studio Features IntelliSense Code completion Error detection Quick fixes Debugging Breakpoints Variable inspection ...
Read MoreC# Program to display temporary file names
The Path.GetTempPath() method in C# retrieves the path of the system's temporary folder where temporary files are stored. This method is useful when your application needs to create temporary files or work with the system's designated temporary directory. Syntax Following is the syntax for using Path.GetTempPath() method − string tempPath = Path.GetTempPath(); Return Value The method returns a string representing the path to the system's temporary directory. On Windows, this is typically the path specified by the TEMP or TMP environment variables. On Unix-like systems, it's usually /tmp/. Using GetTempPath() to Display ...
Read MoreRepresent Int64 as a String in C#
The Int64 data type in C# represents a 64-bit signed integer that can hold values from -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807. Converting an Int64 to a string is a common operation that can be accomplished using several methods, with ToString() being the most straightforward approach. Syntax The basic syntax for converting an Int64 to string using ToString() method − long variable = value; string result = variable.ToString(); You can also use format specifiers with ToString() − string result = variable.ToString("format"); ...
Read MoreDateTime.ToLongDateString() Method in C#
The DateTime.ToLongDateString() method in C# converts a DateTime object to its equivalent long date string representation. This method returns a human-readable date format that includes the day of the week, month name, day, and year, formatted according to the current culture settings. Syntax Following is the syntax − public string ToLongDateString(); Return Value This method returns a string that represents the long date format of the DateTime object. The format follows the current culture's long date pattern, typically in the form "Day, Month Date, Year". Using ToLongDateString() with Specific DateTime The ...
Read More