Found 34494 Articles for Programming

C# program to check whether a given string is Heterogram or not

karthikeya Boyini
Updated on 22-Jun-2020 13:03:33

297 Views

Heterogram for a string means the string isn’t having duplicate letters. For example −Mobile Cry LaptopLoop through each word of the string until the length of the string −for (int i = 0; i < len; i++) {    if (val[str[i] - 'a'] == 0)    val[str[i] - 'a'] = 1;    else    return false; }Above, len is the length of the string.Let us see the complete code −Example Live Demousing System; public class GFG {    static bool checkHeterogram(string str, int len) {       int []val = new int[26];       for (int i ... Read More

How to Convert Hex String to Hex Number in C#?

karthikeya Boyini
Updated on 22-Jun-2020 13:04:03

1K+ Views

Firstly, set the Hex String −string str = "7D";Now, use the Convert.ToSByte() method to convert the Hex string to Hex number −Console.WriteLine(Convert.ToSByte(str, 16));Let us see the complete code −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToSByte(str, 16));       }    } }Output125Another way of converting Hex String to Hex Number −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToInt32(str, 16));       }    } }Output125

How to convert a tuple into an array in C#?

Samual Sam
Updated on 22-Jun-2020 12:53:45

737 Views

Firstly, set a tuple −Tuple t = Tuple.Create(99,53);Now, convert the tuple to an array −int[] arr = new int[]{t.Item1, t.Item2};The following is the code to convert a tuple into an array −Example Live Demousing System; using System.Linq; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          Tuple t = Tuple.Create(99,53);          int[] arr = new int[]{t.Item1, t.Item2};          foreach (int val in arr) {             Console.WriteLine(val);          }       }    }   }Output99 53

How to convert a number from Decimal to Binary using recursion in C#?

karthikeya Boyini
Updated on 22-Jun-2020 12:54:49

364 Views

To get the binary of Decimal, using recursion, firstly set the decimal number −int dec = 30;Now pass the value to a function −public int displayBinary(int dec) { }Now, check the condition until the decimal value is 0 and using recursion get the mod 2 of the decimal num as shown below. The recursive call will call the function again with the dec/2 value −public int displayBinary(int dec) {    int res;    if (dec != 0) {       res = (dec % 2) + 10 * displayBinary(dec / 2);       Console.Write(res);       return ... Read More

Merge two sorted arrays into a list using C#

Samual Sam
Updated on 22-Jun-2020 12:55:32

192 Views

To merge two sorted arrays into a list, firstly set two sorted arrays −int[] array1 = { 1, 2 }; int[] array2 = { 3, 4 };Add it to a list and merge −var list = new List(); for (int i = 0; i < array1.Length; i++) {    list.Add(array1[i]);       list.Add(array2[i]); }Now, use the ToArray() method to convert back into an array as shown below −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] array1 = { 56, 70, 77};       int[] array2 = ... Read More

C# program to check for a string that contains all vowels

karthikeya Boyini
Updated on 22-Jun-2020 12:55:59

384 Views

To check for all vowels, firstly set the condition to check −string res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();Above, we have used the string −string str = "the quick brown fox jumps over the lazy dog";Now, using the Any() method checks whether the string has any vowels or not −if(!res.Any()) Console.WriteLine("No vowels!");Loop through the string to get the vowels −Example Live Demousing System; using System.Linq; public class Program {    public static void Main() {       string str = "the quick brown fox jumps over the lazy dog";       var res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();     ... Read More

C# program to multiply all numbers in the list

Samual Sam
Updated on 22-Jun-2020 12:56:44

811 Views

Firstly, set the list −List myList = new List () {    5,    10,    7 };Now, set the value of a variable to 1 that would help us in multiplying −int prod = 1;Loop through and get the product −foreach(int i in myList) {    prod = prod*i; }The following is the code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       List myList = new List() {          5,          10,          7       };       Console.WriteLine("List: ");       foreach(int i in myList) {          Console.WriteLine(i);       }       int prod = 1;       foreach(int i in myList) {          prod = prod*i;       }       Console.WriteLine("Product: {0}",prod);    } }OutputList: 5 10 7 Product: 350

Foreach in C++ and C#

karthikeya Boyini
Updated on 22-Jun-2020 12:57:28

556 Views

Foreach in C++C++ 11 introduced foreach loop to traverse over each element. Here is an example −Example Live Demo#include using namespace std; int main() {    int myArr[] = { 99, 15, 67 };    // foreach loop    for (int ele : myArr)    cout

Print first m multiples of n in C#

Samual Sam
Updated on 30-Jul-2019 22:30:23

541 Views

To print m multiples of n, first set the value of m and n − int n = 6, m = 1; Now loop through the value of m, increment it and multiply with n on every iteration − while (m

Print number with commas as 1000 separators in C#

Giri Raju
Updated on 22-Jun-2020 12:57:50

1K+ Views

Firstly, set the number as string −string num = "1000000.8765";Now, work around differently for number before and after the decimal −string withoutDecimals = num.Substring(0, num.IndexOf(".")); string withDecimals = num.Substring(num.IndexOf("."));Use the ToString() method to set the format for 1000 separators −ToString("#,##0")The following is the complete code to display number with commas as 1000 separators −Example Live Demousing System; public class Program {    public static void Main() {       string num = "1000000.8765";       string withoutDecimals = num.Substring(0, num.IndexOf("."));       string withDecimals = num.Substring(num.IndexOf("."));       withoutDecimals = Convert.ToInt32(withoutDecimals).ToString("#,##0");       Console.WriteLine(withoutDecimals + withDecimals);    } }Output1,000,000.8765

Advertisements