Found 2745 Articles for Csharp

C# Program to display priority of Thread

Samual Sam
Updated on 19-Jun-2020 11:16:52

236 Views

To show the priority of the thread in C#, use the Priority property.Firstly, use the currentThread property to display information about a thread −Thread thread = Thread.CurrentThread;Now use the thread.Priority property to display the priority of the thread −thread.PriorityExampleLet us see the complete code to show the thread’s priority in C#.Live Demousing System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread thread = Thread.CurrentThread;          thread.Name = "My Thread";          Console.WriteLine("Thread Priority = {0}", thread.Priority);          Console.ReadKey();       }    } }OutputThread Priority = Normal

C# Program to display name of Current Thread

karthikeya Boyini
Updated on 19-Jun-2020 11:17:38

137 Views

To display the name of the current thread in C#, use the Name property.Firstly, use the currentThread property to display information about a thread −Thread thread = Thread.CurrentThread;Now use the thread.Name property to display name of the thread −thread.NameExampleLet us see the complete code show current thread’s name in C#.Live Demousing System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread thread = Thread.CurrentThread;          thread.Name = "My Thread";          Console.WriteLine("Thread Name = {0}", thread.Name);          Console.ReadKey();       }    } }OutputThread Name = My Thread

C# Program to count vowels in a string

Samual Sam
Updated on 19-Jun-2020 11:25:55

6K+ Views

You need to check for both the vowels and consonants, but do not forget to check for both the uppercase as well lowercase.For counting vowels, check for “aeiou” characters separately i.e.if (myStr[i] == 'a' || myStr[i] == 'e' || myStr[i] == 'i' || myStr[i] == 'o' || myStr[i] == 'u' || myStr[i] == 'A' || myStr[i] == 'E' || myStr[i] == 'I' || myStr[i] == 'O' || myStr[i] == 'U') {    vowel_count++; }ExampleThe following is the code to count the number of Vowels in a string.Live Demousing System; public class Demo {    public static void Main() {   ... Read More

C# program to count total set bits in a number

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

189 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 count total set bits in a number, use the following code.Live Demousing System; public class Demo {    public static void Main() {       int cal = 0;       // Binary is 1011       int num = 11;       while (num>0) {          cal += num & 1;          num >>= 1;       }       // 1 bits in 1101 are 3       Console.WriteLine("Total bits: "+cal);    } }OutputTotal bits: 3

C# program to count total bits in a number

Samual Sam
Updated on 19-Jun-2020 09:32:32

368 Views

Let us say the number we have is 12. We have declared and initialized a uint variable by assigning a decimal literal,uint val = 12;The binary representation of 12 is −1100The bits above is 4, therefore to find the total bits, use the Math.log() method −uint res = (uint)Math.Log(val , 2.0) + 1;ExampleYou can try to run the following code to count total bits in a number.Live Demousing System; public class Demo {    public static void Main() {       uint val = 12; // 1100 in binary       uint res = (uint) Math.Log(val, 2.0) + 1;       // 1100 has 4 bits       Console.WriteLine("Total bits: " + res);    } }OutputTotal bits: 4

C# program to count the occurrences of each character

karthikeya Boyini
Updated on 19-Jun-2020 09:33:20

18K+ Views

Firstly, set the string −string str = "Website"; Console.WriteLine("String: "+str);Check for every character in the string and increment a variable that would count the number of occurrences of that character −for (int j = 0; j < str.Length; j++) {    if (str[0] == str[j]) {       cal++;    } }ExampleYou can try to run the following code to count the occurrences of each character.Live Demousing System; public class Demo {    public static void Main() {       string str = "Website";       Console.WriteLine("String: "+str);       while (str.Length > 0) {   ... Read More

C# Program to Count the Number of 1's in the Entered Numbers

Samual Sam
Updated on 19-Jun-2020 09:33:57

382 Views

I have added the numbers using an array −int[] num = new int[] {1, 25, 1, 55, 1};Now loop through and find for 1, if 1 is there, 6 then increment the variable that counts the occurrence −foreach(int j in num) {    if (j == 1) {       cal++;    } }ExampleThe following is the code to count the number of 1’s in the entered number.Live Demousing System; public class Demo {    public static void Main() {       int cal = 0;       int[] num = new int[] {1, 25, 1, 55, ... Read More

C# Program to count number of Vowels and Consonants in a string

karthikeya Boyini
Updated on 19-Jun-2020 09:35:14

501 Views

You need to check for both the vowels and consonants, but do not forget to check for both the uppercase as well lowercase.For counting vowels, check for “aeiou” characters separately i.e.if (myStr[i] == 'a' || myStr[i] == 'e' || myStr[i] == 'i' || myStr[i] == 'o' || myStr[i] == 'u' || myStr[i] == 'A' || myStr[i] == 'E' || myStr[i] == 'I' || myStr[i] == 'O' || myStr[i] == 'U') {    vowel_count++; }For counting consonants, check for other characters in elseif condition −else if ((myStr[i] >= 'a' && myStr[i] = 'A' && myStr[i]

C# program to convert decimal to Octal number

Samual Sam
Updated on 19-Jun-2020 09:36:15

539 Views

Set the decimal number −int decVal = 40;Now take a variable and set the decVal in it. Find the remainder with 8 since octal has base-8 number system and evaluate it in a loop like the following code snippet.while (quot != 0) {    octalVal[i++] = quot % 8;    quot = quot / 8; }ExampleYou can try to run the following code to convert decimal to octal number.Live Demousing System; class Demo {    public static void Main() {       int decVal, quot, i = 1, j;       int[] octalVal = new int[80];     ... Read More

C# Program to convert first character uppercase in a sentence

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

347 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]

Advertisements