Found 34488 Articles for Programming

How to find the length and rank of a jagged array in C#?

Ankith Reddy
Updated on 22-Jun-2020 10:06:48

254 Views

Firstly, set 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 GetLowerBound(), GetUpperBound() and Rank property to get the lenth and rank of the array as shown in the following code ... Read More

Find the Sum of two Binary Numbers without using a method in C#?

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

168 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

How to find the first character of a string in C#?

George John
Updated on 10-Sep-2023 08:18:07

37K+ Views

To get the first character, use the substring() method.Let’s say the following is our string −string str = "Welcome to the Planet!";Now to get the first character, set the value 1 in the substring() method.string res = str.Substring(0, 1);Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       string str = "Welcome to the Planet!";       string res = str.Substring(0, 1);       Console.WriteLine(res);    } }OutputW

How to find the sum of digits of a number using recursion in C#?

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

413 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

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

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

464 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

How to find the first 10 characters of a string in C#?

karthikeya Boyini
Updated on 22-Jun-2020 09:56:07

11K+ Views

To get the first 10 characters, use the substring() method.Let’s say the following is our string −string str = "Cricket is a religion in India!";Now to get the first 10 characters, set the value 10 in the substring() method as shown below −string res = str.Substring(0, 10);Let us see the complete code.Example Live Demousing System; public class Demo {    public static void Main() {       string str = "Cricket is a religion in India!";       string res = str.Substring(0, 10);       Console.WriteLine(res);    } }OutputCricket is

How to find the Average Values of all the Array Elements in C#?

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

148 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);    } }

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

Samual Sam
Updated on 22-Jun-2020 09:56:58

148 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

C# Program to replace a character with asterisks in a sentence

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

943 Views

Use the Replace() method to replace a character with asterisks.Let’s say our string is −string str = "dem* text";To replace it, use the Replace() method −str.Replace('*', 'o');Here is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       string str = "dem* text";       Console.WriteLine("Initial string = " + str);       string res = str.Replace('*', 'o');       // after replacing       Console.WriteLine("After replacing asterisk = " + res.ToString());    } }OutputInitial string = dem* text After replacing asterisk = demo text

C# Program to replace a special character from a String

karthikeya Boyini
Updated on 22-Jun-2020 09:57:38

5K+ Views

Let’s say our string is −string str = "abcd$ef$gh";To replace the special character, use the Replace() method.string res = str.Replace('$', 'k');The following is the complete code to replace character from a string −Example Live Demousing System; public class Program {    public static void Main() {       string str = "abcd$ef$gh";       Console.WriteLine("Initial string = " + str);       string res = str.Replace('$', 'k');       // after replacing       Console.WriteLine("Replaced string = " + res.ToString());    } }OutputInitial string = abcd$ef$gh Replaced string = abcdkefkgh

Advertisements