Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 30 of 151

Print first m multiples of n in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 733 Views

To print the first m multiples of a given number n in C#, we use a loop to iterate and calculate each multiple. This is useful for displaying multiplication tables or generating sequences. Syntax Following is the basic syntax for printing multiples using a loop − for (int i = 1; i

Read More

C# program to multiply all numbers in the list

Samual Sam
Samual Sam
Updated on 17-Mar-2026 1K+ Views

Multiplying all numbers in a list is a common programming task in C#. This can be achieved using several approaches including loops, LINQ, and recursion. The key is to initialize a variable to 1 (the multiplicative identity) and then multiply each element in the list. Syntax Following is the basic syntax for multiplying all numbers in a list using a foreach loop − List numbers = new List { 1, 2, 3 }; int product = 1; foreach (int number in numbers) { product *= number; } Using Foreach Loop ...

Read More

How to write single-line comments in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 379 Views

Single-line comments in C# are created using two forward slashes (//). Everything after the // on that line is treated as a comment and is ignored by the compiler. These comments are useful for explaining code, adding notes, or temporarily disabling a line of code. Syntax Following is the syntax for single-line comments in C# − // This is a single-line comment int variable = 10; // Comment at the end of a line Using Single-Line Comments for Code Documentation Example using System; namespace Demo { class ...

Read More

How to compare two lists for equality in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

Comparing two lists for equality in C# can be done using several approaches. The most common methods include using SequenceEqual() for order-dependent comparison, Except() to find differences, and custom logic for specific comparison requirements. Using SequenceEqual() for Order-Dependent Comparison The SequenceEqual() method from LINQ compares two sequences element by element in the same order − using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List list1 = new List {"A", "B", "C"}; List list2 ...

Read More

Division Operator in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 7K+ Views

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 More

How to find a number using Pythagoras Theorem using C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 646 Views

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 More

How to check if two Strings are anagrams of each other using C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. For example, "silent" and "listen" are anagrams because they contain the same letters in different arrangements. In C#, there are several approaches to check if two strings are anagrams. The most common method is to sort the characters of both strings and compare them for equality. Using Array.Sort() Method The simplest approach is to convert both strings to character arrays, sort them, and then compare the sorted arrays − ...

Read More

Comparing dates using C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 384 Views

Comparing dates in C# is accomplished using the DateTime structure, which provides built-in comparison operators and methods. The DateTime class allows you to compare dates using standard operators like , ==, and specialized methods like Compare(). Syntax Following is the syntax for creating DateTime objects for comparison − DateTime date1 = new DateTime(year, month, day); DateTime date2 = new DateTime(year, month, day); Following are the comparison operators and methods available − // Using comparison operators if (date1 < date2) { } if (date1 > date2) { } if (date1 == date2) { ...

Read More

What is the difference between function overriding and method hiding in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 791 Views

In C#, function overriding and method hiding are two different mechanisms for changing method behavior in derived classes. While both allow a child class to provide its own implementation of a parent class method, they work in fundamentally different ways. Function Overriding Function overriding allows a subclass to provide a specific implementation of a method that is already defined in its parent class. The parent method must be marked as virtual, abstract, or override, and the child method uses the override keyword. Syntax // Parent class public virtual ReturnType MethodName() { } // Child ...

Read More

How to find date difference in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 167 Views

To find the difference between two dates in C#, you can use the DateTime class along with the TimeSpan structure. The DateTime subtraction operation returns a TimeSpan object that represents the time difference between two dates. Syntax Following is the syntax for calculating date difference − DateTime date1 = new DateTime(year, month, day); DateTime date2 = new DateTime(year, month, day); TimeSpan difference = date2 - date1; The TimeSpan object provides various properties to access different units of the difference − difference.Days // Total ...

Read More
Showing 291–300 of 1,507 articles
« Prev 1 28 29 30 31 32 151 Next »
Advertisements