Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 49 of 151

Find frequency of each word in a string in C#

Samual Sam
Samual Sam
Updated on 22-Jun-2020 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 Volume and Surface Area of a Sphere using C#?

Samual Sam
Samual Sam
Updated on 22-Jun-2020 412 Views

For volume and surface area of a sphere, firstly declare a variable with the value of radius.int r = 15;Get the volume f the sphere.// calculating volume of sphere cal_volume = (4.0 / 3) * (22 / 7) * r * r * r;Now the surface area of the sphere is calculated −cal_area = 4 * (22 / 7) * r * r;Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       double cal_area, cal_volume, r;       // radius       ...

Read More

How to find the average of elements of an integer array in C#?

Samual Sam
Samual Sam
Updated on 22-Jun-2020 292 Views

The following is our integer array −int[] myArr = new int[6] {    8,    4,    2,    5,    9,    14 };Firstly, get the length of the array, and loop through the array to find the sum of the elements. After that, divide it with the length.int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) {    sum += myArr[i]; } average = sum / len;Here is the complete codeExample Live Demousing System; public class Program {    public static void Main() {       ...

Read More

How to find the product of two binary numbers using C#?

Samual Sam
Samual Sam
Updated on 22-Jun-2020 668 Views

To find the product of two binary numbers, firstly set them.val1 = 11100; val2 = 10001; Console.WriteLine("Binary one: "+val1); Console.WriteLine("Binary two: "+val2);Now loop through to get the product.while (val2 != 0) {    digit = val2 % 10;    if (digit == 1) {       val1 = val1 * factor;       prod = displayMul(val1, prod);    } else    val1 = val1 * factor;    val2 = val2 / 10;    factor = 10; } Console.WriteLine("Product = {0}", prod);Above a method displayMul() is called with the first binary number.static long displayMul (long val1, long val2) ...

Read More

C# program to check if two lists have at-least one element common

Samual Sam
Samual Sam
Updated on 22-Jun-2020 1K+ Views

Set the first list.int[] arr1 = {    65,    57,    63,    98 };Now, set the second list.int[] arr2 = {    43,    65,    33,    57 };Let us now see the complete code to check if two lists have common elements using == and < operators.Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Program {    public static void Main() {       int[] arr1 = {          65,          57,          63,          98       }; ...

Read More

C# program to print unique values from a list

Samual Sam
Samual Sam
Updated on 22-Jun-2020 6K+ Views

Set the list.List < int > list = new List < int > (); list.Add(99); list.Add(49); list.Add(32);To get unique elements.List myList = list.Distinct().ToList();Here is the complete example to display unique values from a list.Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < int > list = new List < int > ();       list.Add(55);       list.Add(45);       list.Add(55);       list.Add(65);       list.Add(73);       list.Add(24);       list.Add(65);       Console.WriteLine("Initial List..."); ...

Read More

What is the Mutex class in C#?

Samual Sam
Samual Sam
Updated on 22-Jun-2020 2K+ Views

The Mutex class in C# is a synchronization primitive that can also be used for interprocess synchronization.Let us see how to create a new Mutex.private static Mutex m = new Mutex();Let us now see how to initialize a new instance of the Mutex class with a Boolean value.private static Mutex m = new Mutex(true);Now let us see how to initialize a new instance of the Mutex class with a Boolean value and the name of the Mutex.Example Live Demousing System; using System.Threading; public class Demo {    public static void Main() {       Mutex mt = new Mutex(false, ...

Read More

Sorting a String in C#

Samual Sam
Samual Sam
Updated on 22-Jun-2020 7K+ Views

Firstly, set a string array.string[] values = { "tim", "amit", "tom", "jack", "saurav"};Use the Sort() method to sort.Array.Sort(values);Let us see the complete code −Example Live Demousing System; public class Program {    public static void Main() {       string[] values = { "tim", "amit", "tom", "jack", "saurav"};       foreach (string value in values) {          Console.Write(value);          Console.Write(' ');       }       // sorting       Array.Sort(values);       Console.WriteLine("Sorted...");       foreach (string value in values) {          Console.Write(value);          Console.Write(' ');       }       Console.WriteLine();    } }Outputtim amit tom jack saurav Sorted... amit jack saurav tim tom

Read More

C# program to split the Even and Odd integers into different arrays

Samual Sam
Samual Sam
Updated on 22-Jun-2020 2K+ Views

Take two arrays:int[] arr2 = new int[5]; int[] arr3 = new int[5];Now, if the array element gets the remainder 0 on dividing by 2, it is even. Get those elements and add in another array. This loops through the length of the array:if (arr1[i] % 2 == 0) {    arr2[j] = arr1[i]; }In the else condition, you will get the odd elements. Add them to a separate array and display them individually as shown in the below example:Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {     ...

Read More

C# program to find Largest, Smallest, Second Largest, Second Smallest in a List

Samual Sam
Samual Sam
Updated on 22-Jun-2020 2K+ Views

Set the listvar val = new int[] {    99,    35,    26,    87 };Now get the largest number.val.Max(z => z);Smallest numberval.Min(z => z);Second largest numberval.OrderByDescending(z => z).Skip(1).First();Second smallest numberval.OrderBy(z => z).Skip(1).First();The following is the code −Example Live Demousing System; using System.Linq; public class Program {    public static void Main() {       var val = new int[] {          99,          35,          26,          87       };       var maxNum = val.Max(z => z);       ...

Read More
Showing 481–490 of 1,507 articles
« Prev 1 47 48 49 50 51 151 Next »
Advertisements