Csharp Articles - Page 151 of 196

How to check if array contains a duplicate number using C#?

Ankith Reddy
Updated on 22-Jun-2020 10:18:36

1K+ Views

Firstly, set an array −int[] arr = {    87,    55,    23,    87,    45,    23,    98 };Now declare a dictionary and loop through the array and get the count of all the elements. The value you get from the dictionary displays the occurrence of numbers −foreach(var count in arr) {    if (dict.ContainsKey(count))    dict[count]++;    else    dict[count] = 1; }Let us see the complete example −Exampleusing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {             ... Read More

C# program to convert floating to binary

Samual Sam
Updated on 22-Jun-2020 10:19:09

842 Views

Let’s say the following is our float −float n = 50.5f;Take an empty string to display the binary value and loop until the value of our float variable is greater than 1 −string a = ""; while (n >= 1) {    a = (n % 2) + a;    n = n / 2; }Let us see the complete example −Example Live Demousing System; using System.IO; using System.CodeDom.Compiler; namespace Program {    class Demo {       static void Main(string[] args) {          // float to binary          Console.WriteLine("float to binary = ");          float n = 50.5f;          string a = "";          while (n >= 1) {             a = (n % 2) + a;             n = n / 2;          }          Console.Write(a);       }    } }Outputfloat to binary = 1.5781251.156250.31250.6251.250.5

C# program to find K’th smallest element in a 2D array

Arjun Thakur
Updated on 22-Jun-2020 10:07:46

631 Views

Declare a 2D array −int[] a = new int[] {    65,    45,    32,    97,    23,    75,    59 };Let’s say you want the Kth smallest i.e, 5th smallest integer. Sort the array first −Array.Sort(a);To get the 5th smallest element −a[k - 1];Let us see the complete code −Exampleusing System; using System.IO; using System.CodeDom.Compiler; namespace Program {    class Demo {       static void Main(string[] args) {          int[] a = new int[] {             65,             45,   ... Read More

How to check if a string is a valid keyword in C#?

karthikeya Boyini
Updated on 22-Jun-2020 10:08:25

2K+ Views

To check if a string is a valid keyword, use the IsValidIdentifier method.The IsValidIdentifier method checks whether the entered value is an identifier or not. If it’s not an identifier, then it’s a keyword in C#.Let us see an example, wherein we have set the CodeDomProvider and worked with the IsValiddentifier method −CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");Let us see the complete codeLExample Live Demousing System; using System.IO; using System.CodeDom.Compiler; namespace Program {    class Demo {       static void Main(string[] args) {              string str1 = "amit";          string str2 = ... Read More

Mobile

Find free disk space using C#

Ankith Reddy
Updated on 22-Jun-2020 10:08:39

1K+ Views

Firstly, create an instance of DriveInfo −DriveInfo dInfo = new DriveInfo("E");Display free space −Console.WriteLine("Disk Free space = {0}", dInfo.AvailableFreeSpace); Now, use AvailableFreeSpace property and get the percentage of free space −Double pc = (dInfo.AvailableFreeSpace / (float)dInfo.TotalSize) * 100;Here, you will get the percentage of free size in comparison with the total disk space −Console.WriteLine(" Free space (percentage) = {0:0.00}%.", pc);

How to get the nth value of a Fibonacci series using recursion in C#?

Chandu yadav
Updated on 22-Jun-2020 10:13:04

1K+ Views

Create a method to get the nth value with recursion.public int displayFibonacci(int n)Call the method −displayFibonacci(val)On calling, the displayFibonacci() meyhod gets called and calculate the nth value using recursion.public int displayFibonacci(int n) {    if (n == 0) {       return 0;    }    if (n == 1) {       return 1;    } else {       return displayFibonacci(n - 1) + displayFibonacci(n - 2);    } }Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main(string[] args) {       Demo d = ... Read More

How to find the Sum of two Binary Numbers using C#?

karthikeya Boyini
Updated on 22-Jun-2020 10:13:48

2K+ Views

To find the sum of two binary numbers, firstly set them.val1 = 11110; val2 = 11100;Now call the displaySum() method, which created to display the sumL.sum = displaySum(val1, val2);We have set a new array in the method to display each bit of the binary number.long[] sum = new long[30];Now let us see the complete code to calculate the sum of binary numbers as shown in the code below −Example Live Demousing System; class Demo {    public static void Main(string[] args) {       long val1, val2, sum = 0;       val1 = 11110;       val2 ... Read More

Find frequency of each word in a string in C#

Samual Sam
Updated on 22-Jun-2020 10:14:23

859 Views

To find the frequency of each word in a string, you can try to run the following code −Example Live Demousing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < s.Length; i++)       cal[s[i]]++;    }    public static void Main() {       String s = "Football!";       int []cal = new int[maxCHARS];       calculate(s, cal);       for (int i = 0; i < maxCHARS; i++)       if(cal[i] > ... Read More

How to find the size of a variable without using sizeof in C#?

Syed Javed
Updated on 23-Jun-2020 08:17:47

2K+ Views

To get the size of a variable, sizeof is used.int x; x = sizeof(int);To get the size of a variable, without using the sizeof, try the following code −// without using sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length;Here is the complete code.Example Live Demousing System; class Demo {    public static void Main() {       int x;       // using sizeof       x = sizeof(int);       Console.WriteLine(x);       // without using sizeof       byte[] dataBytes = BitConverter.GetBytes(x);       int d = dataBytes.Length;       Console.WriteLine(d);    } }Output4 4

Declare a const array in C#

Ankith Reddy
Updated on 22-Jun-2020 09:57:59

13K+ Views

In C#, use readonly to declare a const array.public static readonly string[] a = { "Car", "Motorbike", "Cab" };In readonly, you can set the value at runtime as well unlike const.Another alternative of achieving what we saw above −public ReadOnlyCollection a { get { return new List { "Car", "Motorbike", "Cab" }.AsReadOnly();}} .NET framework 4.5 brings an improvement to what we saw −public ReadOnlyCollection a { get; } = new ReadOnlyCollection( new string[] { "Car", "Motorbike", "Cab" } );

Advertisements