Calculate Power of a Number Using Recursion in C#

George John
Updated on 20-Jun-2020 15:26:30

1K+ Views

To calculate power of a number using recursion, try the following code.Here, if the power is not equal to 0, then the function call occurs which is eventually recursion −if (p!=0) {    return (n * power(n, p - 1)); }Above, n is the number itself and the power reduces on every iteration as shown below −Example Live Demousing System; using System.IO; public class Demo {    public static void Main(string[] args) {       int n = 5;       int p = 2;       long res;       res = power(n, p);       Console.WriteLine(res);    }    static long power (int n, int p) {       if (p!=0) {          return (n * power(n, p - 1));       }       return 1;    } }Output25

Call Custom Methods in C#

Samual Sam
Updated on 20-Jun-2020 15:25:24

339 Views

To define a custom method in C#, use the following syntax − (Parameter List) {    Method Body }To call a custom method, try to run the following code. It has the checkPalindrome() method which is called to checker whether the binary representation is Palindrome or not −Example Live Demousing System; public class Demo {    public static long funcReverse(long num) {       long myRev = 0;       while (num > 0) {          myRev = 1;       }       return myRev;    }    public static ... Read More

Calculate Fractional Power Using C#

Ankith Reddy
Updated on 20-Jun-2020 15:24:02

609 Views

To calculate fractional power in C#, use the Math.Pow method.The following sets 5 to the power 3.7 −double res = Math.Pow(5, 3.7);The following is the complete example showing how to calculate fractional power in C# −Example Live Demousing System; class Program {    static void Main() {       double res = Math.Pow(5, 3.7);       Console.WriteLine("Result = {0}", res);       Console.ReadLine();    } }OutputResult = 385.646164200006

What are Postfix Operators in C#

karthikeya Boyini
Updated on 20-Jun-2020 15:23:14

962 Views

The increment operator is ++ operator. If used as postfix on a variable, the value of the variable is first returned and then gets incremented by 1. It is called Postfix increment operator. In the same way, the decrement operator works but it decrements by 1.For example,a++;The following is an example showing how to work with postfix operator −Example Live Demousing System; class Program {    static void Main() {       int a, b;       a = 10;       Console.WriteLine(a++);           b = a;       Console.WriteLine(a);       Console.WriteLine(b);    } }Output10 11 11

ICollection Interface in C#

George John
Updated on 20-Jun-2020 15:22:31

2K+ Views

The ICollection interface in C# defines the size, enumerators, and synchronization methods for all nongeneric collections. It is the base interface for classes in the System.Collections namespace.The following are the properties of ICollection interface −Sr.No.Property Name & Description1CountThe number of elements in the ICollection2SyncRootGets an object that useful to synchronize access to the ICollection.The following are the methods of ICollection interface −Sr.No.Method Name & Description1CopyTo(Array^,Int32)The method copies the elements of the ICollection to an Array.2GetEnumerator()The GetEnumerator() method returns an enumerator that iterates through a collection

What Does the IEnumerable Interface Do in C#

Samual Sam
Updated on 20-Jun-2020 15:22:15

12K+ Views

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated.This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.It has a single method −GetEnumerator() − This method returns an enumerator that iterates through a collection.The following is the implementation of the GetEnumerator() method of the IEnumerable interface in C# −IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator) GetEnumerator(); }The following are the extension methods of the IEnumerable interface in C# −Sr.NoMethod Name & Description1AsParallel()Enables parallelization of a ... Read More

What Does the ICloneable Interface Do in C#

Chandu yadav
Updated on 20-Jun-2020 15:22:00

3K+ Views

The ICloneable interface creates a copy of the exisiting object i.e a clone.It only has a single method −Clone() − The clone() method creates a new object that is a copy of the current instance.The following is an example showing how to perform cloning using Icloneable interface −Example Live Demousing System; class Car : ICloneable {    int width;    public Car(int width) {       this.width = width;    }    public object Clone() {       return new Car(this.width);    }    public override string ToString() {       return string.Format("Width of ... Read More

IList Interface in C#

karthikeya Boyini
Updated on 20-Jun-2020 15:21:31

1K+ Views

The IList interface has a non-generic collection of objects that can be individually accessed by index.The following are the properties of interface IList in C# −Sr.NoProperty Name & Description1CountGets the number of elements contained in the ICollection.2isFixedSizeGets a value indicating whether the IList has a fixed size.3isReadOnlyGets a value indicating whether the IList is read-only.4isSynchronizedGets a value indicating whether access to the ICollection is synchronized.5Item(Int32)Gets or sets the element at the specified index.The following are the methods of the IList interface −Sr.NoProperty Name & Description1Add(Obj)Adds an item to the IList.2Clear()Removes all items from the IList3Contains(Obj)Whether the list contains a specific ... Read More

IStructuralComparable Interface in C#

Samual Sam
Updated on 20-Jun-2020 15:21:07

244 Views

The IStructuralComparable interface supports the structural comparison of collection objects. This interface introduced in .NET 4.The following is the syntax −public interface IStructuralComparableIt has a single method −CompareTo(Object, IComparer) − It determines whether the current collection object precedes, occurs in the same position as, or follows another object in the sort order.The compareTo() method determines whether the current collection object is less than, equal to, or greater than the second object in the sort order.Explicit implementations for the IStructuralComparable Interface is provided by −Generic tuple classes (Tuple, Tuple, Tuple,…Array class

Display Numbers in the Form of Triangle using C#

George John
Updated on 20-Jun-2020 15:20:37

252 Views

To display numbers in the form of Triangle, firstly consider a two dimensional array.int[, ] a = new int[5, 5];For a triangle, you need to consider spaces as shown below −1 1 1 1 2 1 1 3 3 1Then loop through to set the triangle with 1s on the left and right as in the following code −Example Live Demousing System; class Demo {    public static void Main() {       // two dimensional array       int[, ] a = new int[5, 5];       for (int i = 0; i < 5; ... Read More

Advertisements