Found 34488 Articles for Programming

C# program to Display Hostname and IP address

karthikeya Boyini
Updated on 19-Jun-2020 11:35:28

1K+ Views

To find the hostname, use the Dns.GetHostName() method in C# −String hostName = string.Empty; hostName = Dns.GetHostName(); Console.WriteLine("Hostname: "+hostName);Now, use the IPHostEntry.AddressList Property to get IP Address −IPHostEntry myIP = Dns.GetHostEntry(hostName); IPAddress[] address = myIP.AddressList;ExampleTry the following code to display hostname and IP address −using System; using System.Net; class Program {    static void Main() {       String hostName = string.Empty;       hostName = Dns.GetHostName();       Console.WriteLine("Hostname: "+hostName);       IPHostEntry myIP = Dns.GetHostEntry(hostName);       IPAddress[] address = myIP.AddressList;       for (int i = 0; i < address.Length; i++) {          Console.WriteLine("IP Address {1} : ",address[i].ToString());       }       Console.ReadLine();    } }

C# program to find node in Linked List

Samual Sam
Updated on 19-Jun-2020 10:52:04

427 Views

Firstly, create a new linked list −LinkedList myList = new LinkedList();Now add some elements in the linked list −// Add 6 elements in the linked list myList.AddLast("P"); myList.AddLast("Q"); myList.AddLast("R"); myList.AddLast("S"); myList.AddLast("T"); myList.AddLast("U");Let’s now find a node and add a new node after that −LinkedListNode node = myList.Find("R"); myList.AddAfter(node, "ADDED");ExampleYou can try to run the following code to find a node in the linked list.Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       LinkedList myList = new LinkedList();       // Add 6 elements in the linked list       myList.AddLast("P");   ... Read More

C# program to count upper and lower case characters in a given string

karthikeya Boyini
Updated on 19-Jun-2020 10:52:36

913 Views

To count uppercase characters in a string, check the following condition −myStr[i]>='A' && myStr[i]='a' && myStr[i]

C# Program to find all substrings in a string

Samual Sam
Updated on 19-Jun-2020 10:57:17

2K+ Views

Use the substring() method in C# to find all substrings in a string.Let’s say our string is −XyzLoop through the length of the string and use the Substring function from the beginning to the end of the string −for (int start = 0; start

C# program to display factors of entered number

karthikeya Boyini
Updated on 19-Jun-2020 11:15:47

940 Views

Firstly, enter the number for which you want the factors −Console.WriteLine("Enter the Number:"); n = int.Parse(Console.ReadLine());After that, loop through to find the factors −for (i = 1; i

C# Program to display priority of Thread

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

263 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

165 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

7K+ 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

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

403 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

Advertisements