Karthikeya Boyini has Published 2383 Articles

C# program to count total set bits in a number

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 09:31:40

210 Views

The number for our example is 11 i.e. binary −1101The total set bits are 3 in 1101; to find it, use a loop till it’s not equal to 0. Here, our num is 11 i.e. decimal −while (num>0) {    cal += num & 1;    num >>= 1; }ExampleTo ... Read More

C# Program to Convert Decimal to Binary

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 09:28:41

916 Views

Let’ s say you have set the decimal to be −decVal = 34; Console.WriteLine("Decimal: {0}", decVal);Use the ToString() method for the values you get as a binary number for the decimal value −while (decVal >= 1) {    val = decVal / 2;    a += (decVal % 2).ToString();   ... Read More

C# Program to convert first character uppercase in a sentence

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 09:27:24

375 Views

Let us say the following is your string −String str = "Welcome to our website!";Create a char array of the string included above using the ToCharArray() method:char []ch = str.ToCharArray();To convert the first character to uppercase −if (ch[i] >= 'a' && ch[i] = 'a' && val[i] = 'A' && val[i] ... Read More

C# program to check if a string is palindrome or not

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 09:05:35

11K+ Views

To check if a string is palindrome or not, you need to first find the reverse of the string using −Array.reverse()After that use the equals() method to match the original string with the reversed. If the result is true, that would mean the string is Palindrome.ExampleLet us try the complete ... Read More

C# Program to Check Whether the Entered Number is an Armstrong Number or Not

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 09:04:36

882 Views

For an Armstrong number, let us say a number has 3 digits, then the sum of cube of its digits is equal to the number itself.For example, 153 is equal to −1³ + 3³ + 5³To check for it using C#, check the value and find its remainder. Here “val” ... Read More

C# program to count the number of words in a string

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 09:03:23

6K+ Views

Let us first declare the string −string str = "Hello World!";Now loop through the complete string and find the whitespace or tab or newline character −while (a

C# program to convert binary string to Integer

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 09:02:11

759 Views

Use the Convert.ToInt32 class to fulfill your purpose of converting a binary string to an integer.Let’s say our binary string is −string str = "1001";Now each char is parsed −try {    //Parse each char of the passed string    val = Int32.Parse(str1[i].ToString());    if (val == 1)     ... Read More

C# Program to Convert Character case

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 09:01:03

391 Views

Let’s say your string is −str = "AMIT";To convert the above uppercase string in lowercase, use the ToLower() method −Console.WriteLine("Converted to LowerCase : {0}", str.ToLower());ExampleThe following is the code in C# to convert character case.Live Demousing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {     ... Read More

C# Example for Single Inheritance

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 08:58:59

3K+ Views

The following is an example of Single Inheritance in C#. In the example, the base class is Father and declared like the following code snippet −class Father {    public void Display() {       Console.WriteLine("Display");    } }Our derived class is Son and is declared below −class Son ... Read More

C# Generics vs C++ Templates

karthikeya Boyini

karthikeya Boyini

Updated on 19-Jun-2020 08:56:11

376 Views

C# Generics and C++ Templates provide support for parameterized types. The following are the differences −FlexibilityC++ Templates are more flexible than C# GenericsExplicit specializationExplicit specialization is not supported by C#Type ParameterThe type parameter cannot be used as the base class for the generic type in C#C# does not allow type ... Read More

Advertisements