Find Average Values of All Array Elements in C#

Arjun Thakur
Updated on 22-Jun-2020 09:55:47

247 Views

To find the average values, firstly set an arrayLint[] myArr = new int[10] {    45,    23,    55,    15,    8,    4,    2,    5,    9,    14 };Now find the sum and divide it by the length of the array to get the average.int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) {    sum += myArr[i];    average = sum / len; }Let us see the complete code −Exampleusing System; public class Program {    public static void Main() {       int[] myArr = new int[10] {          45,          23,          55,          15,          8,          4,          2,          5,          9,          14       };       int len = myArr.Length;       int sum = 0;       int average = 0;       for (int i = 0; i < len; i++) {          sum += myArr[i];       }       average = sum / len;       Console.WriteLine("Sum = " + sum);       Console.WriteLine("Average Of integer elements = " + average);    } }

Find Sum of Digits Using Recursion in C#

Chandu yadav
Updated on 22-Jun-2020 09:54:46

631 Views

To get the sum of digits using recursion, set a method in C# that calculates the sum.static int sum(int n) {    if (n != 0) {       return (n % 10 + sum(n / 10));    } else {       return 0;    }The above method returns the sum and checks it until the entered number is not equal to 0.The recursive call returns the sum of digits o every recursive call −return (n % 10 + sum(n / 10));Let us see the complete code −Example Live Demousing System; class Demo {    public static void ... Read More

Find Product of Two Binary Numbers Using C#

Samual Sam
Updated on 22-Jun-2020 09:54:11

644 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

Find the Sum of Two Binary Numbers in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:52:29

314 Views

First, declare and initialize two variables with the binary numbers.val1 = 11010; val2 = 10100; Console.WriteLine("Binary one: " + val1); Console.WriteLine("Binary two: " + val2);To get the sum, loop until both the value are 0.while (val1 != 0 || val2 != 0) {    sum[i++] = (val1 % 10 + val2 % 10 + rem) % 2;    rem = (val1 % 10 + val2 % 10 + rem) / 2;    val1 = val1 / 10;    val2 = val2 / 10; }Now, let us see the complete code to find the sum of two binary numbers.Example Live Demousing ... Read More

Threads and Thread Synchronization in C#

Samual Sam
Updated on 22-Jun-2020 09:51:18

1K+ Views

Using Synchronization, you can synchronize access to resources in multithreaded applications.A mutex can be used to synchronize threads across processes Use it to prevent the simultaneous execution of a block of code by more than one thread at a time.C# lock statement is used to ensure that a block of code runs without interruption by other threads. A Mutual-exclusion lock is obtained for a given object for the duration of the code block.Thread pool in C# is a collection of threads. It is used to perform tasks in the background. When a thread completes a task, it is sent to ... Read More

Check Validity of a Password in C#

George John
Updated on 22-Jun-2020 09:51:05

8K+ Views

For validity of a password, you need to recall the concept when your create a password to signup to a website.While creating a password, you may have seen the validation requirements on a website like a password should be strong and have −Min 8 char and max 14 charOne upper caseOne special charOne lower caseNo white spaceLet us see how to check the conditions one by one.Min 8 char and max 14 charif (passwd.Length < 8 || passwd.Length > 14) return false;One upper caseif (!passwd.Any(char.IsUpper)) return false;Atleast one lower caseif (!passwd.Any(char.IsLower)) return false;No white spaceif (passwd.Contains(" ")) return false;Check for ... Read More

Clone or Copy a List in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:50:44

489 Views

To copy or clone a C# list, firstly set a list.List < string > myList = new List < string > (); myList.Add("One"); myList.Add("Two");Now declare a string array and use the CopyTo() method to copy.string[] arr = new string[10]; myList.CopyTo(arr);Let us see the complete code to copy a list into a one-dimensional array.Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > myList = new List < string > ();       myList.Add("One");       myList.Add("Two");       myList.Add("Three");       myList.Add("Four"); ... Read More

Find Length of Jagged Array Using a Property

Ankith Reddy
Updated on 22-Jun-2020 09:49:50

1K+ Views

Firstly, declare and initialize a jagged array.int[][] arr = new int[][] { new int[] {    0,    0 }, new int[] {    1,    2 }, new int[] {    2,    4 }, new int[] {    3,    6 }, new int[] {    4,    8 } };Now use the length property to get the length.arr.LengthThe following is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       int[][] arr = new int[][]{new int[]{0,0},new int[]{1,2}, new int[]{2,4},new int[]{ 3, 6 }, new       int[]{ 4, 8 } };       // Length       Console.WriteLine("Length:"+arr.Length);       Console.WriteLine("Upper Bound: {0}",arr.GetUpperBound(0).ToString());       Console.WriteLine("Lower Bound: {0}",arr.GetLowerBound(0).ToString());       Console.WriteLine("Dimensions of Array : " + arr.Rank);    } }OutputLength:5 Upper Bound: 4 Lower Bound: 0 Dimensions of Array : 1

Check if Two Lists Have at Least One Element Common in C#

Samual Sam
Updated on 22-Jun-2020 09:49:07

984 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

Check Values in List Greater Than Given Value in C#

Arjun Thakur
Updated on 22-Jun-2020 09:47:12

2K+ Views

Let’s say you need to find the elements in the following list to be greater than 80.int[] arr = new int[] {55, 100, 87, 45};For that, loop until the array length. Here, res = 80 i.e. the given element.for (int i = 0; i < arr.Length; i++) {    if(arr[i] res) {                Console.WriteLine(arr[i]);             }          }       }    } }

Advertisements