Found 34488 Articles for Programming

C# program to find the sum of digits of a number using Recursion

karthikeya Boyini
Updated on 19-Jun-2020 12:13:42

237 Views

Let’s say we have set the number for which we will find the sum of digits −int val = 789; Console.WriteLine("Number:", val);The following will find the sum of digits by entering the number and checking it recursively −public int addFunc(int val) {    if (val != 0) {       return (val % 10 + addFunc(val / 10));    } else {       return 0;    } }ExampleThe following is our code to find the sum of digits of a number using Recursion in C#.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {   ... Read More

C# Program to find whether the Number is Divisible by 2

Samual Sam
Updated on 19-Jun-2020 12:14:32

6K+ Views

If the remainder of the number when it is divided by 2 is 0, then it would be divisible by 2.Let’s say our number is 5, we will check it using the following if-else −// checking if the number is divisible by 2 or not if (num % 2 == 0) {    Console.WriteLine("Divisible by 2 "); } else {    Console.WriteLine("Not divisible by 2"); }ExampleThe following is an example to find whether the number is divisible by 2 or not.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class MyApplication {       static ... Read More

C# Program to Create a Thread Pool

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

491 Views

For a thread pool, create more than two functions and queue methods for execution.Firstly, create a method like −public void one(object o) {    for (int i = 0; i

C# Program to create a Simple Thread

Samual Sam
Updated on 19-Jun-2020 11:38:02

433 Views

To create a thread, I have created a function −public void myThread() {    for (int i = 0; i < 3; i++) {       Console.WriteLine("My Thread");    } }The above function is called to create a thread and a new ThreadStart delegate is created −Demo d = new Demo(); Thread thread = new Thread(new ThreadStart(d.myThread));ExampleCreate a simple thread using the following code.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; class Demo {    public void myThread() {       for (int i = 0; i < 3; i++) {         ... Read More

C# program to find the maximum of three numbers

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

10K+ Views

Firstly, let’s set the three numbers −int num1, num2, num3; // set the value of the three numbers num1 = 10; num2 = 20; num3 = 50;Now check the first number with the second number. If num1 > num2, then check num1 with num3. If num1 is greater than num3, that would mean the largest number is num1.ExampleYou can try to run the following code to find the maximum of three numbers.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          int ... Read More

C# program to split and join a string

Samual Sam
Updated on 19-Jun-2020 11:39:37

2K+ Views

To split and join a string in C#, use the split() and join() method. Let us say the following is our string −string str = "This is our Demo String";To split the string, we will use the split() method −var arr = str.Split(' ');Now to join, use the join() method and join rest of the string. Here, we have skipped the part of the string using the skip() method −string rest = string.Join(" ", arr.Skip(1));ExampleYou can try to run the following code in C# to split and join a string.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo ... Read More

C# Program to perform Celsius to Fahrenheit Conversion

karthikeya Boyini
Updated on 19-Jun-2020 11:41:12

6K+ Views

Firstly, set the Celsius temperature −double celsius = 36; Console.WriteLine("Celsius: " + celsius);Now convert it into Fahrenheit:fahrenheit = (celsius * 9) / 5 + 32;You can try to run the following code to convert Celsius to Fahrenheit.ExampleLive Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          double fahrenheit;          double celsius = 36;          Console.WriteLine("Celsius: " + celsius);          fahrenheit = (celsius * 9) / 5 + 32;          Console.WriteLine("Fahrenheit: " + fahrenheit);          Console.ReadLine();       }    } }OutputCelsius: 36 Fahrenheit: 96.8

C# Program to perform all Basic Arithmetic Operations

Samual Sam
Updated on 19-Jun-2020 11:44:15

7K+ Views

Basic Arithmetic Operators in C#, include the following −OperatorDescription+Adds two operands-Subtracts the second operand from the first*Multiplies both operands/Divides the numerator by de-numerator%Modulus Operator and remainder of after an integer division++Increment operator increases integer value by one--Decrement operator decreases integer value by oneTo add, use the Addition Operator −num1 + num2;In the same way, it works for Subtraction, Multiplication, Division, and other operators.ExampleLet us see a complete example to learn how to implement Arithmetic operators in C#.Live Demousing System; namespace Sample {    class Demo {       static void Main(string[] args) {          int num1 ... Read More

C# Program to Pause a Thread

karthikeya Boyini
Updated on 19-Jun-2020 11:46:34

7K+ Views

To pause a thread in C#, use the sleep() method.You need to set the milliseconds for which you want the thread to pause, for example, for 5 seconds, use −Thread.Sleep(5000);ExampleLet us see how to loop through and set the sleep method to pause the thread.Live Demousing System; using System.Threading; namespace Sample {    class Demo {       static void Main(string[] args) {          for (int i = 0; i < 10; i++) {             Console.WriteLine("Sleep for 1 second!");             Thread.Sleep(1000);          } ... Read More

C# Program to pass Parameter to a Thread

Samual Sam
Updated on 19-Jun-2020 11:47:31

4K+ Views

To work with threads, add the following namespace in your code −using System.Threading;Firstly, you need to create a new thread in C# −Thread thread = new Thread(threadDemo);Above, threadDemo is our thread function.Now pass a parameter to the thread −thread.Start(str);The parameter set above is −String str = "Hello World!";ExampleLet us see the complete code to pass a parameter to a thread in C#.Live Demousing System; using System.Threading; namespace Sample {    class Demo {       static void Main(string[] args) {          String str = "Hello World!";          // new thread       ... Read More

Advertisements